home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / cccp.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  265KB  |  9,805 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2.    Copyright (C) 1986, 87, 89, 92, 93, 1994 Free Software Foundation, Inc.
  3.    Written by Paul Rubin, June 1986
  4.    Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!  */
  23.  
  24. typedef unsigned char U_CHAR;
  25.  
  26. #ifdef EMACS
  27. #define NO_SHORTNAMES
  28. #include "../src/config.h"
  29. #ifdef open
  30. #undef open
  31. #undef read
  32. #undef write
  33. #endif /* open */
  34. #endif /* EMACS */
  35.  
  36. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  37.    for the sake of machines with limited C compilers.  */
  38. #ifndef EMACS
  39. #include "config.h"
  40. #endif /* not EMACS */
  41.  
  42. #ifndef STANDARD_INCLUDE_DIR
  43. #define STANDARD_INCLUDE_DIR "/usr/include"
  44. #endif
  45.  
  46. #ifndef LOCAL_INCLUDE_DIR
  47. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  48. #endif
  49.  
  50. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  51. #ifdef __STDC__
  52. #define PTR_INT_TYPE ptrdiff_t
  53. #else
  54. #define PTR_INT_TYPE long
  55. #endif
  56. #endif /* 0 */
  57.  
  58. #include "pcp.h"
  59.  
  60. #ifndef STDC_VALUE
  61. #define STDC_VALUE 1
  62. #endif
  63.  
  64. /* By default, colon separates directories in a path.  */
  65. #ifndef PATH_SEPARATOR
  66. #define PATH_SEPARATOR ':'
  67. #endif
  68.  
  69. /* In case config.h defines these.  */
  70. #undef bcopy
  71. #undef bzero
  72. #undef bcmp
  73.  
  74. #include <sys/types.h>
  75. #include <sys/stat.h>
  76. #include <ctype.h>
  77. #include <stdio.h>
  78. #include <signal.h>
  79.  
  80. #ifndef VMS
  81. #ifndef USG
  82. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  83. #include <sys/resource.h>
  84. #else
  85. #include <time.h>
  86. #include <fcntl.h>
  87. #endif /* USG */
  88. #endif /* not VMS */
  89.  
  90. /* This defines "errno" properly for VMS, and gives us EACCES. */
  91. #include <errno.h>
  92.  
  93. /* VMS-specific definitions */
  94. #ifdef VMS
  95. #include <time.h>
  96. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  97. #include <descrip.h>
  98. #define O_RDONLY    0    /* Open arg for Read/Only  */
  99. #define O_WRONLY    1    /* Open arg for Write/Only */
  100. #define read(fd,buf,size)    VMS_read (fd,buf,size)
  101. #define write(fd,buf,size)    VMS_write (fd,buf,size)
  102. #define open(fname,mode,prot)    VMS_open (fname,mode,prot)
  103. #define fopen(fname,mode)    VMS_fopen (fname,mode)
  104. #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
  105. #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
  106. static char * VMS_strncat ();
  107. static int VMS_read ();
  108. static int VMS_write ();
  109. static int VMS_open ();
  110. static FILE * VMS_fopen ();
  111. static FILE * VMS_freopen ();
  112. static void hack_vms_include_specification ();
  113. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  114. #define ino_t vms_ino_t
  115. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  116. #ifdef __GNUC__
  117. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  118. #endif /* __GNUC__ */
  119. #endif /* VMS */
  120.   
  121. extern char *index ();
  122. extern char *rindex ();
  123.  
  124. #ifndef O_RDONLY
  125. #define O_RDONLY 0
  126. #endif
  127.  
  128. #undef MIN
  129. #undef MAX
  130. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  131. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  132.  
  133. /* Find the largest host integer type and set its size and type.  */
  134.  
  135. #ifndef HOST_BITS_PER_WIDE_INT
  136.  
  137. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  138. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  139. #define HOST_WIDE_INT long
  140. #else
  141. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  142. #define HOST_WIDE_INT int
  143. #endif
  144.  
  145. #endif
  146.  
  147. #ifndef S_ISREG
  148. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  149. #endif
  150.  
  151. #ifndef S_ISDIR
  152. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  153. #endif
  154.  
  155. /* Define a generic NULL if one hasn't already been defined.  */
  156.  
  157. #ifndef NULL
  158. #define NULL 0
  159. #endif
  160.  
  161. #ifndef GENERIC_PTR
  162. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  163. #define GENERIC_PTR void *
  164. #else
  165. #define GENERIC_PTR char *
  166. #endif
  167. #endif
  168.  
  169. #ifndef NULL_PTR
  170. #define NULL_PTR ((GENERIC_PTR)0)
  171. #endif
  172.  
  173. #ifndef INCLUDE_LEN_FUDGE
  174. #define INCLUDE_LEN_FUDGE 0
  175. #endif
  176.  
  177. /* Forward declarations.  */
  178.  
  179. char *xmalloc ();
  180. void error ();
  181. void warning ();
  182.  
  183. /* External declarations.  */
  184.  
  185. extern char *getenv ();
  186. extern FILE *fdopen ();
  187. extern char *version_string;
  188. extern struct tm *localtime ();
  189. extern int sys_nerr;
  190. #if defined(bsd4_4) || defined(__NetBSD__)
  191. extern const char *const sys_errlist[];
  192. #else
  193. extern char *sys_errlist[];
  194. #endif
  195. extern int parse_escape ();
  196.  
  197. #ifndef errno
  198. extern int errno;
  199. #endif
  200.  
  201. /* Forward declarations.  */
  202.  
  203. struct directive;
  204. struct file_buf;
  205. struct arglist;
  206. struct argdata;
  207.  
  208. #if defined(USG) || defined(VMS)
  209. #ifndef BSTRING
  210. void bcopy ();
  211. void bzero ();
  212. int bcmp ();
  213. #endif
  214. #endif
  215.  
  216. /* These functions are declared to return int instead of void since they
  217.    are going to be placed in a table and some old compilers have trouble with
  218.    pointers to functions returning void.  */
  219.  
  220. static int do_define ();
  221. static int do_line ();
  222. static int do_include ();
  223. static int do_undef ();
  224. static int do_error ();
  225. static int do_pragma ();
  226. static int do_ident ();
  227. static int do_if ();
  228. static int do_xifdef ();
  229. static int do_else ();
  230. static int do_elif ();
  231. static int do_endif ();
  232. static int do_sccs ();
  233. static int do_once ();
  234. static int do_assert ();
  235. static int do_unassert ();
  236. static int do_warning ();
  237.  
  238. static void add_import ();
  239. static void append_include_chain ();
  240. static void deps_output ();
  241. static void make_undef ();
  242. static void make_definition ();
  243. static void make_assertion ();
  244. static void path_include ();
  245. static void initialize_builtins ();
  246. static void initialize_char_syntax ();
  247. static void dump_arg_n ();
  248. static void dump_defn_1 ();
  249. static void delete_macro ();
  250. static void trigraph_pcp ();
  251. static void rescan ();
  252. static void finclude ();
  253. static void validate_else ();
  254. static int comp_def_part ();
  255. static void error_from_errno ();
  256. static void error_with_line ();
  257. void pedwarn ();
  258. void pedwarn_with_line ();
  259. static void pedwarn_with_file_and_line ();
  260. static void fatal ();
  261. void fancy_abort ();
  262. static void pfatal_with_name ();
  263. static void perror_with_name ();
  264. static void pipe_closed ();
  265. static void print_containing_files ();
  266. static int lookup_import ();
  267. static int redundant_include_p ();
  268. static is_system_include ();
  269. static struct file_name_map *read_name_map ();
  270. static char *read_filename_string ();
  271. static int open_include_file ();
  272. static int check_preconditions ();
  273. static void pcfinclude ();
  274. static void pcstring_used ();
  275. static void write_output ();
  276. static int check_macro_name ();
  277. static int compare_defs ();
  278. static int compare_token_lists ();
  279. static int eval_if_expression ();
  280. static int discard_comments ();
  281. static int change_newlines ();
  282. static int line_for_error ();
  283. static int hashf ();
  284. static int file_size_and_mode ();
  285.  
  286. static struct arglist *read_token_list ();
  287. static void free_token_list ();
  288.  
  289. static struct hashnode *install ();
  290. struct hashnode *lookup ();
  291.  
  292. static struct assertion_hashnode *assertion_install ();
  293. static struct assertion_hashnode *assertion_lookup ();
  294.  
  295. static char *xrealloc ();
  296. static char *xcalloc ();
  297. static char *savestring ();
  298.  
  299. static void delete_assertion ();
  300. static void macroexpand ();
  301. static void dump_all_macros ();
  302. static void conditional_skip ();
  303. static void skip_if_group ();
  304. static void output_line_command ();
  305.  
  306. /* Last arg to output_line_command.  */
  307. enum file_change_code {same_file, enter_file, leave_file};
  308.  
  309. static int grow_outbuf ();
  310. static int handle_directive ();
  311. static void memory_full ();
  312.  
  313. static U_CHAR *macarg1 ();
  314. static char *macarg ();
  315.  
  316. static U_CHAR *skip_to_end_of_comment ();
  317. static U_CHAR *skip_quoted_string ();
  318. static U_CHAR *skip_paren_group ();
  319. static char *quote_string ();
  320.  
  321. static char *check_precompiled ();
  322. /* static struct macrodef create_definition ();    [moved below] */
  323. static void dump_single_macro ();
  324. static void output_dots ();
  325.  
  326. #ifndef FAILURE_EXIT_CODE
  327. #define FAILURE_EXIT_CODE 33    /* gnu cc command understands this */
  328. #endif
  329.  
  330. #ifndef SUCCESS_EXIT_CODE
  331. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  332. #endif
  333.  
  334. /* Name under which this program was invoked.  */
  335.  
  336. static char *progname;
  337.  
  338. /* Nonzero means use extra default include directories for C++.  */
  339.  
  340. static int cplusplus;
  341.  
  342. /* Nonzero means handle cplusplus style comments */
  343.  
  344. static int cplusplus_comments;
  345.  
  346. /* Nonzero means handle #import, for objective C.  */
  347.  
  348. static int objc;
  349.  
  350. /* Nonzero means this is an assembly file, and allow
  351.    unknown directives, which could be comments.  */
  352.  
  353. static int lang_asm;
  354.  
  355. /* Current maximum length of directory names in the search path
  356.    for include files.  (Altered as we get more of them.)  */
  357.  
  358. static int max_include_len;
  359.  
  360. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  361.  
  362. static int for_lint = 0;
  363.  
  364. /* Nonzero means copy comments into the output file.  */
  365.  
  366. static int put_out_comments = 0;
  367.  
  368. /* Nonzero means don't process the ANSI trigraph sequences.  */
  369.  
  370. static int no_trigraphs = 0;
  371.  
  372. /* Nonzero means print the names of included files rather than
  373.    the preprocessed output.  1 means just the #include "...",
  374.    2 means #include <...> as well.  */
  375.  
  376. static int print_deps = 0;
  377.  
  378. /* Nonzero if missing .h files in -M output are assumed to be generated
  379.    files and not errors.  */
  380.  
  381. static int print_deps_missing_files = 0;
  382.  
  383. /* Nonzero means print names of header files (-H).  */
  384.  
  385. static int print_include_names = 0;
  386.  
  387. /* Nonzero means don't output line number information.  */
  388.  
  389. static int no_line_commands;
  390.  
  391. /* dump_only means inhibit output of the preprocessed text
  392.              and instead output the definitions of all user-defined
  393.              macros in a form suitable for use as input to cccp.
  394.    dump_names means pass #define and the macro name through to output.
  395.    dump_definitions means pass the whole definition (plus #define) through
  396. */
  397.  
  398. static enum {dump_none, dump_only, dump_names, dump_definitions}
  399.      dump_macros = dump_none;
  400.  
  401. /* Nonzero means pass all #define and #undef directives which we actually
  402.    process through to the output stream.  This feature is used primarily
  403.    to allow cc1 to record the #defines and #undefs for the sake of
  404.    debuggers which understand about preprocessor macros, but it may
  405.    also be useful with -E to figure out how symbols are defined, and
  406.    where they are defined.  */
  407. static int debug_output = 0;
  408.  
  409. /* Nonzero indicates special processing used by the pcp program.  The
  410.    special effects of this mode are: 
  411.      
  412.      Inhibit all macro expansion, except those inside #if directives.
  413.  
  414.      Process #define directives normally, and output their contents 
  415.      to the output file.
  416.  
  417.      Output preconditions to pcp_outfile indicating all the relevant
  418.      preconditions for use of this file in a later cpp run.
  419. */
  420. static FILE *pcp_outfile;
  421.  
  422. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  423.    macro expansion is done, and preconditions are output for all macro
  424.    uses requiring them. */
  425. static int pcp_inside_if;
  426.  
  427. /* Nonzero means never to include precompiled files.
  428.    This is 1 since there's no way now to make precompiled files,
  429.    so it's not worth testing for them.  */
  430. static int no_precomp = 1;
  431.  
  432. /* Nonzero means give all the error messages the ANSI standard requires.  */
  433.  
  434. int pedantic;
  435.  
  436. /* Nonzero means try to make failure to fit ANSI C an error.  */
  437.  
  438. static int pedantic_errors;
  439.  
  440. /* Nonzero means don't print warning messages.  -w.  */
  441.  
  442. static int inhibit_warnings = 0;
  443.  
  444. /* Nonzero means warn if slash-star appears in a comment.  */
  445.  
  446. static int warn_comments;
  447.  
  448. /* Nonzero means warn if a macro argument is (or would be)
  449.    stringified with -traditional.  */
  450.  
  451. static int warn_stringify;
  452.  
  453. /* Nonzero means warn if there are any trigraphs.  */
  454.  
  455. static int warn_trigraphs;
  456.  
  457. /* Nonzero means warn if #import is used.  */
  458.  
  459. static int warn_import = 1;
  460.  
  461. /* Nonzero means turn warnings into errors.  */
  462.  
  463. static int warnings_are_errors;
  464.  
  465. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  466.  
  467. int traditional;
  468.  
  469. /* Nonzero causes output not to be done,
  470.    but directives such as #define that have side effects
  471.    are still obeyed.  */
  472.  
  473. static int no_output;
  474.  
  475. /* Nonzero means this file was included with a -imacros or -include
  476.    command line and should not be recorded as an include file.  */
  477.  
  478. static int no_record_file;
  479.  
  480. /* Nonzero means that we have finished processing the command line options.
  481.    This flag is used to decide whether or not to issue certain errors
  482.    and/or warnings.  */
  483.  
  484. static int done_initializing = 0;
  485.  
  486. /* Line where a newline was first seen in a string constant.  */
  487.  
  488. static int multiline_string_line = 0;
  489.  
  490. /* I/O buffer structure.
  491.    The `fname' field is nonzero for source files and #include files
  492.    and for the dummy text used for -D and -U.
  493.    It is zero for rescanning results of macro expansion
  494.    and for expanding macro arguments.  */
  495. #define INPUT_STACK_MAX 400
  496. static struct file_buf {
  497.   char *fname;
  498.   /* Filename specified with #line command.  */
  499.   char *nominal_fname;
  500.   /* Record where in the search path this file was found.
  501.      For #include_next.  */
  502.   struct file_name_list *dir;
  503.   int lineno;
  504.   int length;
  505.   U_CHAR *buf;
  506.   U_CHAR *bufp;
  507.   /* Macro that this level is the expansion of.
  508.      Included so that we can reenable the macro
  509.      at the end of this level.  */
  510.   struct hashnode *macro;
  511.   /* Value of if_stack at start of this file.
  512.      Used to prohibit unmatched #endif (etc) in an include file.  */
  513.   struct if_stack *if_stack;
  514.   /* Object to be freed at end of input at this level.  */
  515.   U_CHAR *free_ptr;
  516.   /* True if this is a header file included using <FILENAME>.  */
  517.   char system_header_p;
  518. } instack[INPUT_STACK_MAX];
  519.  
  520. static int last_error_tick;       /* Incremented each time we print it.  */
  521. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  522.  
  523. /* Current nesting level of input sources.
  524.    `instack[indepth]' is the level currently being read.  */
  525. static int indepth = -1;
  526. #define CHECK_DEPTH(code) \
  527.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  528.     {                                    \
  529.       error_with_line (line_for_error (instack[indepth].lineno),    \
  530.                "macro or `#include' recursion too deep");    \
  531.       code;                                \
  532.     }
  533.  
  534. /* Current depth in #include directives that use <...>.  */
  535. static int system_include_depth = 0;
  536.  
  537. typedef struct file_buf FILE_BUF;
  538.  
  539. /* The output buffer.  Its LENGTH field is the amount of room allocated
  540.    for the buffer, not the number of chars actually present.  To get
  541.    that, subtract outbuf.buf from outbuf.bufp. */
  542.  
  543. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  544. static FILE_BUF outbuf;
  545.  
  546. /* Grow output buffer OBUF points at
  547.    so it can hold at least NEEDED more chars.  */
  548.  
  549. #define check_expand(OBUF, NEEDED)  \
  550.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  551.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  552.  
  553. struct file_name_list
  554.   {
  555.     struct file_name_list *next;
  556.     char *fname;
  557.     /* If the following is nonzero, it is a macro name.
  558.        Don't include the file again if that macro is defined.  */
  559.     U_CHAR *control_macro;
  560.     /* If the following is nonzero, it is a C-language system include
  561.        directory.  */
  562.     int c_system_include_path;
  563.     /* Mapping of file names for this directory.  */
  564.     struct file_name_map *name_map;
  565.     /* Non-zero if name_map is valid.  */
  566.     int got_name_map;
  567.   };
  568.  
  569. /* #include "file" looks in source file dir, then stack. */
  570. /* #include <file> just looks in the stack. */
  571. /* -I directories are added to the end, then the defaults are added. */
  572. /* The */
  573. static struct default_include {
  574.   char *fname;            /* The name of the directory.  */
  575.   int cplusplus;        /* Only look here if we're compiling C++.  */
  576.   int cxx_aware;        /* Includes in this directory don't need to
  577.                    be wrapped in extern "C" when compiling
  578.                    C++.  */
  579. } include_defaults_array[]
  580. #ifdef INCLUDE_DEFAULTS
  581.   = INCLUDE_DEFAULTS;
  582. #else
  583.   = {
  584.     /* Pick up GNU C++ specific include files.  */
  585.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  586. #ifdef CROSS_COMPILE
  587.     /* This is the dir for fixincludes.  Put it just before
  588.        the files that we fix.  */
  589.     { GCC_INCLUDE_DIR, 0, 0 },
  590.     /* For cross-compilation, this dir name is generated
  591.        automatically in Makefile.in.  */
  592.     { CROSS_INCLUDE_DIR, 0, 0 },
  593.     /* This is another place that the target system's headers might be.  */
  594.     { TOOL_INCLUDE_DIR, 0, 1 },
  595. #else /* not CROSS_COMPILE */
  596.     /* This should be /usr/local/include and should come before
  597.        the fixincludes-fixed header files.  */
  598.     { LOCAL_INCLUDE_DIR, 0, 1 },
  599.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  600.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  601.     { TOOL_INCLUDE_DIR, 0, 1 },
  602.     /* This is the dir for fixincludes.  Put it just before
  603.        the files that we fix.  */
  604.     { GCC_INCLUDE_DIR, 0, 0 },
  605.     /* Some systems have an extra dir of include files.  */
  606. #ifdef SYSTEM_INCLUDE_DIR
  607.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  608. #endif
  609.     { STANDARD_INCLUDE_DIR, 0, 0 },
  610. #endif /* not CROSS_COMPILE */
  611.     { 0, 0, 0 }
  612.     };
  613. #endif /* no INCLUDE_DEFAULTS */
  614.  
  615. /* The code looks at the defaults through this pointer, rather than through
  616.    the constant structure above.  This pointer gets changed if an environment
  617.    variable specifies other defaults.  */
  618. static struct default_include *include_defaults = include_defaults_array;
  619.  
  620. static struct file_name_list *include = 0;    /* First dir to search */
  621.     /* First dir to search for <file> */
  622. /* This is the first element to use for #include <...>.
  623.    If it is 0, use the entire chain for such includes.  */
  624. static struct file_name_list *first_bracket_include = 0;
  625. /* This is the first element in the chain that corresponds to
  626.    a directory of system header files.  */
  627. static struct file_name_list *first_system_include = 0;
  628. static struct file_name_list *last_include = 0;    /* Last in chain */
  629.  
  630. /* Chain of include directories to put at the end of the other chain.  */
  631. static struct file_name_list *after_include = 0;
  632. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  633.  
  634. /* Chain to put at the start of the system include files.  */
  635. static struct file_name_list *before_system = 0;
  636. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  637.  
  638. /* List of included files that contained #pragma once.  */
  639. static struct file_name_list *dont_repeat_files = 0;
  640.  
  641. /* List of other included files.
  642.    If ->control_macro if nonzero, the file had a #ifndef
  643.    around the entire contents, and ->control_macro gives the macro name.  */
  644. static struct file_name_list *all_include_files = 0;
  645.  
  646. /* Directory prefix that should replace `/usr' in the standard
  647.    include file directories.  */
  648. static char *include_prefix;
  649.  
  650. /* Global list of strings read in from precompiled files.  This list
  651.    is kept in the order the strings are read in, with new strings being
  652.    added at the end through stringlist_tailp.  We use this list to output
  653.    the strings at the end of the run. 
  654. */
  655. static STRINGDEF *stringlist;
  656. static STRINGDEF **stringlist_tailp = &stringlist;
  657.  
  658.  
  659. /* Structure returned by create_definition */
  660. typedef struct macrodef MACRODEF;
  661. struct macrodef
  662. {
  663.   struct definition *defn;
  664.   U_CHAR *symnam;
  665.   int symlen;
  666. };
  667.  
  668. static struct macrodef create_definition ();
  669.  
  670.  
  671. /* Structure allocated for every #define.  For a simple replacement
  672.    such as
  673.        #define foo bar ,
  674.    nargs = -1, the `pattern' list is null, and the expansion is just
  675.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  676.    e.g.,
  677.        #define getchar() getc (stdin) .
  678.    When there are args, the expansion is the replacement text with the
  679.    args squashed out, and the reflist is a list describing how to
  680.    build the output from the input: e.g., "3 chars, then the 1st arg,
  681.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  682.    The chars here come from the expansion.  Whatever is left of the
  683.    expansion after the last arg-occurrence is copied after that arg.
  684.    Note that the reflist can be arbitrarily long---
  685.    its length depends on the number of times the arguments appear in
  686.    the replacement text, not how many args there are.  Example:
  687.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  688.    pattern list
  689.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  690.    where (x, y) means (nchars, argno). */
  691.  
  692. typedef struct definition DEFINITION;
  693. struct definition {
  694.   int nargs;
  695.   int length;            /* length of expansion string */
  696.   int predefined;        /* True if the macro was builtin or */
  697.                 /* came from the command line */
  698.   U_CHAR *expansion;
  699.   int line;            /* Line number of definition */
  700.   char *file;            /* File of definition */
  701.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  702.   struct reflist {
  703.     struct reflist *next;
  704.     char stringify;        /* nonzero if this arg was preceded by a
  705.                    # operator. */
  706.     char raw_before;        /* Nonzero if a ## operator before arg. */
  707.     char raw_after;        /* Nonzero if a ## operator after arg. */
  708.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  709.     int nchars;            /* Number of literal chars to copy before
  710.                    this arg occurrence.  */
  711.     int argno;            /* Number of arg to substitute (origin-0) */
  712.   } *pattern;
  713.   union {
  714.     /* Names of macro args, concatenated in reverse order
  715.        with comma-space between them.
  716.        The only use of this is that we warn on redefinition
  717.        if this differs between the old and new definitions.  */
  718.     U_CHAR *argnames;
  719.   } args;
  720. };
  721.  
  722. /* different kinds of things that can appear in the value field
  723.    of a hash node.  Actually, this may be useless now. */
  724. union hashval {
  725.   int ival;
  726.   char *cpval;
  727.   DEFINITION *defn;
  728.   KEYDEF *keydef;
  729. };
  730.  
  731. /*
  732.  * special extension string that can be added to the last macro argument to 
  733.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  734.  *         #define wow(a, b...)        process (b, a, b)
  735.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  736.  *        { wow (one, two); }    ->    { process (two, one, two); }
  737.  * if this "rest_arg" is used with the concat token '##' and if it is not
  738.  * supplied then the token attached to with ## will not be outputted.  Ex:
  739.  *         #define wow (a, b...)        process (b ## , a, ## b)
  740.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  741.  *        { wow (one); }        ->    { process (one); {
  742.  */
  743. static char rest_extension[] = "...";
  744. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  745.  
  746. /* The structure of a node in the hash table.  The hash table
  747.    has entries for all tokens defined by #define commands (type T_MACRO),
  748.    plus some special tokens like __LINE__ (these each have their own
  749.    type, and the appropriate code is run when that type of node is seen.
  750.    It does not contain control words like "#define", which are recognized
  751.    by a separate piece of code. */
  752.  
  753. /* different flavors of hash nodes --- also used in keyword table */
  754. enum node_type {
  755.  T_DEFINE = 1,    /* the `#define' keyword */
  756.  T_INCLUDE,    /* the `#include' keyword */
  757.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  758.  T_IMPORT,      /* the `#import' keyword */
  759.  T_IFDEF,    /* the `#ifdef' keyword */
  760.  T_IFNDEF,    /* the `#ifndef' keyword */
  761.  T_IF,        /* the `#if' keyword */
  762.  T_ELSE,    /* `#else' */
  763.  T_PRAGMA,    /* `#pragma' */
  764.  T_ELIF,    /* `#elif' */
  765.  T_UNDEF,    /* `#undef' */
  766.  T_LINE,    /* `#line' */
  767.  T_ERROR,    /* `#error' */
  768.  T_WARNING,    /* `#warning' */
  769.  T_ENDIF,    /* `#endif' */
  770.  T_SCCS,    /* `#sccs', used on system V.  */
  771.  T_IDENT,    /* `#ident', used on system V.  */
  772.  T_ASSERT,    /* `#assert', taken from system V.  */
  773.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  774.  T_SPECLINE,    /* special symbol `__LINE__' */
  775.  T_DATE,    /* `__DATE__' */
  776.  T_FILE,    /* `__FILE__' */
  777.  T_BASE_FILE,    /* `__BASE_FILE__' */
  778.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  779.  T_VERSION,    /* `__VERSION__' */
  780.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  781.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  782.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  783.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  784.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  785.  T_TIME,    /* `__TIME__' */
  786.  T_CONST,    /* Constant value, used by `__STDC__' */
  787.  T_MACRO,    /* macro defined by `#define' */
  788.  T_DISABLED,    /* macro temporarily turned off for rescan */
  789.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  790.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  791.  T_UNUSED    /* Used for something not defined.  */
  792.  };
  793.  
  794. struct hashnode {
  795.   struct hashnode *next;    /* double links for easy deletion */
  796.   struct hashnode *prev;
  797.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  798.                    chain is kept, in case the node is the head
  799.                    of the chain and gets deleted. */
  800.   enum node_type type;        /* type of special token */
  801.   int length;            /* length of token, for quick comparison */
  802.   U_CHAR *name;            /* the actual name */
  803.   union hashval value;        /* pointer to expansion, or whatever */
  804. };
  805.  
  806. typedef struct hashnode HASHNODE;
  807.  
  808. /* Some definitions for the hash table.  The hash function MUST be
  809.    computed as shown in hashf () below.  That is because the rescan
  810.    loop computes the hash value `on the fly' for most tokens,
  811.    in order to avoid the overhead of a lot of procedure calls to
  812.    the hashf () function.  Hashf () only exists for the sake of
  813.    politeness, for use when speed isn't so important. */
  814.  
  815. #define HASHSIZE 1403
  816. static HASHNODE *hashtab[HASHSIZE];
  817. #define HASHSTEP(old, c) ((old << 2) + c)
  818. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  819.  
  820. /* Symbols to predefine.  */
  821.  
  822. #ifdef CPP_PREDEFINES
  823. static char *predefs = CPP_PREDEFINES;
  824. #else
  825. static char *predefs = "";
  826. #endif
  827.  
  828. /* We let tm.h override the types used here, to handle trivial differences
  829.    such as the choice of unsigned int or long unsigned int for size_t.
  830.    When machines start needing nontrivial differences in the size type,
  831.    it would be best to do something here to figure out automatically
  832.    from other information what type to use.  */
  833.  
  834. /* The string value for __SIZE_TYPE__.  */
  835.  
  836. #ifndef SIZE_TYPE
  837. #define SIZE_TYPE "long unsigned int"
  838. #endif
  839.  
  840. /* The string value for __PTRDIFF_TYPE__.  */
  841.  
  842. #ifndef PTRDIFF_TYPE
  843. #define PTRDIFF_TYPE "long int"
  844. #endif
  845.  
  846. /* The string value for __WCHAR_TYPE__.  */
  847.  
  848. #ifndef WCHAR_TYPE
  849. #define WCHAR_TYPE "int"
  850. #endif
  851. char * wchar_type = WCHAR_TYPE;
  852. #undef WCHAR_TYPE
  853.  
  854. /* The string value for __USER_LABEL_PREFIX__ */
  855.  
  856. #ifndef USER_LABEL_PREFIX
  857. #define USER_LABEL_PREFIX ""
  858. #endif
  859.  
  860. /* The string value for __REGISTER_PREFIX__ */
  861.  
  862. #ifndef REGISTER_PREFIX
  863. #define REGISTER_PREFIX ""
  864. #endif
  865.  
  866. /* In the definition of a #assert name, this structure forms
  867.    a list of the individual values asserted.
  868.    Each value is itself a list of "tokens".
  869.    These are strings that are compared by name.  */
  870.  
  871. struct tokenlist_list {
  872.   struct tokenlist_list *next;
  873.   struct arglist *tokens;
  874. };
  875.  
  876. struct assertion_hashnode {
  877.   struct assertion_hashnode *next;    /* double links for easy deletion */
  878.   struct assertion_hashnode *prev;
  879.   /* also, a back pointer to this node's hash
  880.      chain is kept, in case the node is the head
  881.      of the chain and gets deleted. */
  882.   struct assertion_hashnode **bucket_hdr;
  883.   int length;            /* length of token, for quick comparison */
  884.   U_CHAR *name;            /* the actual name */
  885.   /* List of token-sequences.  */
  886.   struct tokenlist_list *value;
  887. };
  888.  
  889. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  890.  
  891. /* Some definitions for the hash table.  The hash function MUST be
  892.    computed as shown in hashf below.  That is because the rescan
  893.    loop computes the hash value `on the fly' for most tokens,
  894.    in order to avoid the overhead of a lot of procedure calls to
  895.    the hashf function.  hashf only exists for the sake of
  896.    politeness, for use when speed isn't so important. */
  897.  
  898. #define ASSERTION_HASHSIZE 37
  899. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  900.  
  901. /* Nonzero means inhibit macroexpansion of what seem to be
  902.    assertion tests, in rescan.  For #if.  */
  903. static int assertions_flag;
  904.  
  905. /* `struct directive' defines one #-directive, including how to handle it.  */
  906.  
  907. struct directive {
  908.   int length;            /* Length of name */
  909.   int (*func)();        /* Function to handle directive */
  910.   char *name;            /* Name of directive */
  911.   enum node_type type;        /* Code which describes which directive. */
  912.   char angle_brackets;        /* Nonzero => <...> is special.  */
  913.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  914.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  915. };
  916.  
  917. /* Here is the actual list of #-directives, most-often-used first.  */
  918.  
  919. static struct directive directive_table[] = {
  920.   {  6, do_define, "define", T_DEFINE, 0, 1},
  921.   {  2, do_if, "if", T_IF},
  922.   {  5, do_xifdef, "ifdef", T_IFDEF},
  923.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  924.   {  5, do_endif, "endif", T_ENDIF},
  925.   {  4, do_else, "else", T_ELSE},
  926.   {  4, do_elif, "elif", T_ELIF},
  927.   {  4, do_line, "line", T_LINE},
  928.   {  7, do_include, "include", T_INCLUDE, 1},
  929.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  930.   {  6, do_include, "import", T_IMPORT, 1},
  931.   {  5, do_undef, "undef", T_UNDEF},
  932.   {  5, do_error, "error", T_ERROR},
  933.   {  7, do_warning, "warning", T_WARNING},
  934. #ifdef SCCS_DIRECTIVE
  935.   {  4, do_sccs, "sccs", T_SCCS},
  936. #endif
  937.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  938.   {  5, do_ident, "ident", T_IDENT},
  939.   {  6, do_assert, "assert", T_ASSERT},
  940.   {  8, do_unassert, "unassert", T_UNASSERT},
  941.   {  -1, 0, "", T_UNUSED},
  942. };
  943.  
  944. /* When a directive handler is called,
  945.    this points to the # that started the directive.  */
  946. U_CHAR *directive_start;
  947.  
  948. /* table to tell if char can be part of a C identifier. */
  949. U_CHAR is_idchar[256];
  950. /* table to tell if char can be first char of a c identifier. */
  951. U_CHAR is_idstart[256];
  952. /* table to tell if c is horizontal space.  */
  953. U_CHAR is_hor_space[256];
  954. /* table to tell if c is horizontal or vertical space.  */
  955. static U_CHAR is_space[256];
  956.  
  957. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  958. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  959.   
  960. static int errors = 0;            /* Error counter for exit code */
  961.  
  962. /* Name of output file, for error messages.  */
  963. static char *out_fname;
  964.  
  965. /* Zero means dollar signs are punctuation.
  966.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  967.    This must be 0 for correct processing of this ANSI C program:
  968.     #define foo(a) #a
  969.     #define lose(b) foo (b)
  970.     #define test$
  971.     lose (test)    */
  972. static int dollars_in_ident;
  973. #ifndef DOLLARS_IN_IDENTIFIERS
  974. #define DOLLARS_IN_IDENTIFIERS 1
  975. #endif
  976.  
  977. static FILE_BUF expand_to_temp_buffer ();
  978.  
  979. static DEFINITION *collect_expansion ();
  980.  
  981. /* Stack of conditionals currently in progress
  982.    (including both successful and failing conditionals).  */
  983.  
  984. struct if_stack {
  985.   struct if_stack *next;    /* for chaining to the next stack frame */
  986.   char *fname;        /* copied from input when frame is made */
  987.   int lineno;            /* similarly */
  988.   int if_succeeded;        /* true if a leg of this if-group
  989.                     has been passed through rescan */
  990.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  991.                    this is the macro name tested.  */
  992.   enum node_type type;        /* type of last directive seen in this group */
  993. };
  994. typedef struct if_stack IF_STACK_FRAME;
  995. static IF_STACK_FRAME *if_stack = NULL;
  996.  
  997. /* Buffer of -M output.  */
  998. static char *deps_buffer;
  999.  
  1000. /* Number of bytes allocated in above.  */
  1001. static int deps_allocated_size;
  1002.  
  1003. /* Number of bytes used.  */
  1004. static int deps_size;
  1005.  
  1006. /* Number of bytes since the last newline.  */
  1007. static int deps_column;
  1008.  
  1009. /* Nonzero means -I- has been seen,
  1010.    so don't look for #include "foo" the source-file directory.  */
  1011. static int ignore_srcdir;
  1012.  
  1013. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1014.    retrying if necessary.  Return a negative value if an error occurs,
  1015.    otherwise return the actual number of bytes read,
  1016.    which must be LEN unless end-of-file was reached.  */
  1017.  
  1018. static int
  1019. safe_read (desc, ptr, len)
  1020.      int desc;
  1021.      char *ptr;
  1022.      int len;
  1023. {
  1024.   int left = len;
  1025.   while (left > 0) {
  1026.     int nchars = read (desc, ptr, left);
  1027.     if (nchars < 0)
  1028.       {
  1029. #ifdef EINTR
  1030.     if (errno == EINTR)
  1031.       continue;
  1032. #endif
  1033.     return nchars;
  1034.       }
  1035.     if (nchars == 0)
  1036.       break;
  1037.     ptr += nchars;
  1038.     left -= nchars;
  1039.   }
  1040.   return len - left;
  1041. }
  1042.  
  1043. /* Write LEN bytes at PTR to descriptor DESC,
  1044.    retrying if necessary, and treating any real error as fatal.  */
  1045.  
  1046. static void
  1047. safe_write (desc, ptr, len)
  1048.      int desc;
  1049.      char *ptr;
  1050.      int len;
  1051. {
  1052.   while (len > 0) {
  1053.     int written = write (desc, ptr, len);
  1054.     if (written < 0)
  1055.       {
  1056. #ifdef EINTR
  1057.     if (errno == EINTR)
  1058.       continue;
  1059. #endif
  1060.     pfatal_with_name (out_fname);
  1061.       }
  1062.     ptr += written;
  1063.     len -= written;
  1064.   }
  1065. }
  1066.  
  1067. int
  1068. main (argc, argv)
  1069.      int argc;
  1070.      char **argv;
  1071. {
  1072.   int st_mode;
  1073.   long st_size;
  1074.   char *in_fname;
  1075.   char *p;
  1076.   int f, i;
  1077.   FILE_BUF *fp;
  1078.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1079.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1080.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1081.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1082.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1083.  
  1084.   /* Record the option used with each element of pend_assertions.
  1085.      This is preparation for supporting more than one option for making
  1086.      an assertion.  */
  1087.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1088.   int inhibit_predefs = 0;
  1089.   int no_standard_includes = 0;
  1090.   int no_standard_cplusplus_includes = 0;
  1091.   int missing_newline = 0;
  1092.  
  1093.   /* Non-0 means don't output the preprocessed program.  */
  1094.   int inhibit_output = 0;
  1095.   /* Non-0 means -v, so print the full set of include dirs.  */
  1096.   int verbose = 0;
  1097.  
  1098.   /* File name which deps are being written to.
  1099.      This is 0 if deps are being written to stdout.  */
  1100.   char *deps_file = 0;
  1101.   /* Fopen file mode to open deps_file with.  */
  1102.   char *deps_mode = "a";
  1103.   /* Stream on which to print the dependency information.  */
  1104.   FILE *deps_stream = 0;
  1105.   /* Target-name to write with the dependency information.  */
  1106.   char *deps_target = 0;
  1107.  
  1108. #ifdef RLIMIT_STACK
  1109.   /* Get rid of any avoidable limit on stack size.  */
  1110.   {
  1111.     struct rlimit rlim;
  1112.  
  1113.     /* Set the stack limit huge so that alloca (particularly stringtab
  1114.      * in dbxread.c) does not fail. */
  1115.     getrlimit (RLIMIT_STACK, &rlim);
  1116.     rlim.rlim_cur = rlim.rlim_max;
  1117.     setrlimit (RLIMIT_STACK, &rlim);
  1118.   }
  1119. #endif /* RLIMIT_STACK defined */
  1120.  
  1121. #ifdef SIGPIPE
  1122.   signal (SIGPIPE, pipe_closed);
  1123. #endif
  1124.  
  1125.   p = argv[0] + strlen (argv[0]);
  1126.   while (p != argv[0] && p[-1] != '/') --p;
  1127.   progname = p;
  1128.  
  1129. #ifdef VMS
  1130.   {
  1131.     /* Remove directories from PROGNAME.  */
  1132.     char *s;
  1133.  
  1134.     progname = savestring (argv[0]);
  1135.  
  1136.     if (!(s = rindex (progname, ']')))
  1137.       s = rindex (progname, ':');
  1138.     if (s)
  1139.       strcpy (progname, s+1);
  1140.     if (s = rindex (progname, '.'))
  1141.       *s = '\0';
  1142.   }
  1143. #endif
  1144.  
  1145.   in_fname = NULL;
  1146.   out_fname = NULL;
  1147.  
  1148.   /* Initialize is_idchar to allow $.  */
  1149.   dollars_in_ident = 1;
  1150.   initialize_char_syntax ();
  1151.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1152.  
  1153.   no_line_commands = 0;
  1154.   no_trigraphs = 1;
  1155.   dump_macros = dump_none;
  1156.   no_output = 0;
  1157.   cplusplus = 0;
  1158.   cplusplus_comments = 0;
  1159.  
  1160.   bzero ((char *) pend_files, argc * sizeof (char *));
  1161.   bzero ((char *) pend_defs, argc * sizeof (char *));
  1162.   bzero ((char *) pend_undefs, argc * sizeof (char *));
  1163.   bzero ((char *) pend_assertions, argc * sizeof (char *));
  1164.   bzero ((char *) pend_includes, argc * sizeof (char *));
  1165.  
  1166.   /* Process switches and find input file name.  */
  1167.  
  1168.   for (i = 1; i < argc; i++) {
  1169.     if (argv[i][0] != '-') {
  1170.       if (out_fname != NULL)
  1171.     fatal ("Usage: %s [switches] input output", argv[0]);
  1172.       else if (in_fname != NULL)
  1173.     out_fname = argv[i];
  1174.       else
  1175.     in_fname = argv[i];
  1176.     } else {
  1177.       switch (argv[i][1]) {
  1178.  
  1179.       case 'i':
  1180.     if (!strcmp (argv[i], "-include")) {
  1181.       if (i + 1 == argc)
  1182.         fatal ("Filename missing after `-include' option");
  1183.       else
  1184.         pend_includes[i] = argv[i+1], i++;
  1185.     }
  1186.     if (!strcmp (argv[i], "-imacros")) {
  1187.       if (i + 1 == argc)
  1188.         fatal ("Filename missing after `-imacros' option");
  1189.       else
  1190.         pend_files[i] = argv[i+1], i++;
  1191.     }
  1192.     if (!strcmp (argv[i], "-iprefix")) {
  1193.       if (i + 1 == argc)
  1194.         fatal ("Filename missing after `-iprefix' option");
  1195.       else
  1196.         include_prefix = argv[++i];
  1197.     }
  1198.     if (!strcmp (argv[i], "-isystem")) {
  1199.       struct file_name_list *dirtmp;
  1200.  
  1201.       if (i + 1 == argc)
  1202.         fatal ("Filename missing after `-isystem' option");
  1203.  
  1204.       dirtmp = (struct file_name_list *)
  1205.         xmalloc (sizeof (struct file_name_list));
  1206.       dirtmp->next = 0;
  1207.       dirtmp->control_macro = 0;
  1208.       dirtmp->c_system_include_path = 1;
  1209.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
  1210.       strcpy (dirtmp->fname, argv[++i]);
  1211.       dirtmp->got_name_map = 0;
  1212.  
  1213.       if (before_system == 0)
  1214.         before_system = dirtmp;
  1215.       else
  1216.         last_before_system->next = dirtmp;
  1217.       last_before_system = dirtmp; /* Tail follows the last one */
  1218.     }
  1219.     /* Add directory to end of path for includes,
  1220.        with the default prefix at the front of its name.  */
  1221.     if (!strcmp (argv[i], "-iwithprefix")) {
  1222.       struct file_name_list *dirtmp;
  1223.       char *prefix;
  1224.  
  1225.       if (include_prefix != 0)
  1226.         prefix = include_prefix;
  1227.       else {
  1228.         prefix = savestring (GCC_INCLUDE_DIR);
  1229.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1230.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1231.           prefix[strlen (prefix) - 7] = 0;
  1232.       }
  1233.  
  1234.       dirtmp = (struct file_name_list *)
  1235.         xmalloc (sizeof (struct file_name_list));
  1236.       dirtmp->next = 0;    /* New one goes on the end */
  1237.       dirtmp->control_macro = 0;
  1238.       dirtmp->c_system_include_path = 0;
  1239.       if (i + 1 == argc)
  1240.         fatal ("Directory name missing after `-iwithprefix' option");
  1241.  
  1242.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1243.                         + strlen (prefix) + 1);
  1244.       strcpy (dirtmp->fname, prefix);
  1245.       strcat (dirtmp->fname, argv[++i]);
  1246.       dirtmp->got_name_map = 0;
  1247.  
  1248.       if (after_include == 0)
  1249.         after_include = dirtmp;
  1250.       else
  1251.         last_after_include->next = dirtmp;
  1252.       last_after_include = dirtmp; /* Tail follows the last one */
  1253.     }
  1254.     /* Add directory to main path for includes,
  1255.        with the default prefix at the front of its name.  */
  1256.     if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1257.       struct file_name_list *dirtmp;
  1258.       char *prefix;
  1259.  
  1260.       if (include_prefix != 0)
  1261.         prefix = include_prefix;
  1262.       else {
  1263.         prefix = savestring (GCC_INCLUDE_DIR);
  1264.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1265.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1266.           prefix[strlen (prefix) - 7] = 0;
  1267.       }
  1268.  
  1269.       dirtmp = (struct file_name_list *)
  1270.         xmalloc (sizeof (struct file_name_list));
  1271.       dirtmp->next = 0;    /* New one goes on the end */
  1272.       dirtmp->control_macro = 0;
  1273.       dirtmp->c_system_include_path = 0;
  1274.       if (i + 1 == argc)
  1275.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1276.  
  1277.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1278.                         + strlen (prefix) + 1);
  1279.       strcpy (dirtmp->fname, prefix);
  1280.       strcat (dirtmp->fname, argv[++i]);
  1281.       dirtmp->got_name_map = 0;
  1282.  
  1283.       append_include_chain (dirtmp, dirtmp);
  1284.     }
  1285.     /* Add directory to end of path for includes.  */
  1286.     if (!strcmp (argv[i], "-idirafter")) {
  1287.       struct file_name_list *dirtmp;
  1288.  
  1289.       dirtmp = (struct file_name_list *)
  1290.         xmalloc (sizeof (struct file_name_list));
  1291.       dirtmp->next = 0;    /* New one goes on the end */
  1292.       dirtmp->control_macro = 0;
  1293.       dirtmp->c_system_include_path = 0;
  1294.       if (i + 1 == argc)
  1295.         fatal ("Directory name missing after `-idirafter' option");
  1296.       else
  1297.         dirtmp->fname = argv[++i];
  1298.       dirtmp->got_name_map = 0;
  1299.  
  1300.       if (after_include == 0)
  1301.         after_include = dirtmp;
  1302.       else
  1303.         last_after_include->next = dirtmp;
  1304.       last_after_include = dirtmp; /* Tail follows the last one */
  1305.     }
  1306.     break;
  1307.  
  1308.       case 'o':
  1309.     if (out_fname != NULL)
  1310.       fatal ("Output filename specified twice");
  1311.     if (i + 1 == argc)
  1312.       fatal ("Filename missing after -o option");
  1313.     out_fname = argv[++i];
  1314.     if (!strcmp (out_fname, "-"))
  1315.       out_fname = "";
  1316.     break;
  1317.  
  1318.       case 'p':
  1319.     if (!strcmp (argv[i], "-pedantic"))
  1320.       pedantic = 1;
  1321.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1322.       pedantic = 1;
  1323.       pedantic_errors = 1;
  1324.     } else if (!strcmp (argv[i], "-pcp")) {
  1325.       char *pcp_fname = argv[++i];
  1326.       pcp_outfile = 
  1327.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1328.          ? fopen (pcp_fname, "w")
  1329.          : fdopen (dup (fileno (stdout)), "w"));
  1330.       if (pcp_outfile == 0)
  1331.         pfatal_with_name (pcp_fname);
  1332.       no_precomp = 1;
  1333.     }
  1334.     break;
  1335.  
  1336.       case 't':
  1337.     if (!strcmp (argv[i], "-traditional")) {
  1338.       traditional = 1;
  1339.       if (dollars_in_ident > 0)
  1340.         dollars_in_ident = 1;
  1341.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1342.       no_trigraphs = 0;
  1343.     }
  1344.     break;
  1345.  
  1346.       case 'l':
  1347.     if (! strcmp (argv[i], "-lang-c"))
  1348.       cplusplus = 0, cplusplus_comments = 0, objc = 0;
  1349.     if (! strcmp (argv[i], "-lang-c++"))
  1350.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1351.     if (! strcmp (argv[i], "-lang-c-c++-comments"))
  1352.       cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1353.     if (! strcmp (argv[i], "-lang-objc"))
  1354.       objc = 1, cplusplus = 0, cplusplus_comments = 1;
  1355.     if (! strcmp (argv[i], "-lang-objc++"))
  1356.       objc = 1, cplusplus = 1, cplusplus_comments = 1;
  1357.      if (! strcmp (argv[i], "-lang-asm"))
  1358.        lang_asm = 1;
  1359.      if (! strcmp (argv[i], "-lint"))
  1360.        for_lint = 1;
  1361.     break;
  1362.  
  1363.       case '+':
  1364.     cplusplus = 1, cplusplus_comments = 1;
  1365.     break;
  1366.  
  1367.       case 'w':
  1368.     inhibit_warnings = 1;
  1369.     break;
  1370.  
  1371.       case 'W':
  1372.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1373.       warn_trigraphs = 1;
  1374.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1375.       warn_trigraphs = 0;
  1376.     else if (!strcmp (argv[i], "-Wcomment"))
  1377.       warn_comments = 1;
  1378.     else if (!strcmp (argv[i], "-Wno-comment"))
  1379.       warn_comments = 0;
  1380.     else if (!strcmp (argv[i], "-Wcomments"))
  1381.       warn_comments = 1;
  1382.     else if (!strcmp (argv[i], "-Wno-comments"))
  1383.       warn_comments = 0;
  1384.     else if (!strcmp (argv[i], "-Wtraditional"))
  1385.       warn_stringify = 1;
  1386.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1387.       warn_stringify = 0;
  1388.     else if (!strcmp (argv[i], "-Wimport"))
  1389.       warn_import = 1;
  1390.     else if (!strcmp (argv[i], "-Wno-import"))
  1391.       warn_import = 0;
  1392.     else if (!strcmp (argv[i], "-Werror"))
  1393.       warnings_are_errors = 1;
  1394.     else if (!strcmp (argv[i], "-Wno-error"))
  1395.       warnings_are_errors = 0;
  1396.     else if (!strcmp (argv[i], "-Wall"))
  1397.       {
  1398.         warn_trigraphs = 1;
  1399.         warn_comments = 1;
  1400.       }
  1401.     break;
  1402.  
  1403.       case 'M':
  1404.     /* The style of the choices here is a bit mixed.
  1405.        The chosen scheme is a hybrid of keeping all options in one string
  1406.        and specifying each option in a separate argument:
  1407.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1408.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1409.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1410.        as extensible.  */
  1411.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1412.        This can be relaxed in the future without breaking anything.
  1413.        The converse isn't true.  */
  1414.  
  1415.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1416.     if (!strcmp (argv[i], "-MG"))
  1417.       {
  1418.         print_deps_missing_files = 1;
  1419.         break;
  1420.       }
  1421.     if (!strcmp (argv[i], "-M"))
  1422.       print_deps = 2;
  1423.     else if (!strcmp (argv[i], "-MM"))
  1424.       print_deps = 1;
  1425.     else if (!strcmp (argv[i], "-MD"))
  1426.       print_deps = 2;
  1427.     else if (!strcmp (argv[i], "-MMD"))
  1428.       print_deps = 1;
  1429.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1430.     if (!strcmp (argv[i], "-MD")
  1431.         || !strcmp (argv[i], "-MMD")) {
  1432.       i++;
  1433.       deps_file = argv[i];
  1434.       deps_mode = "w";
  1435.     } else {
  1436.       /* For -M and -MM, write deps on standard output
  1437.          and suppress the usual output.  */
  1438.       deps_stream = stdout;
  1439.       inhibit_output = 1;
  1440.     }      
  1441.     break;
  1442.  
  1443.       case 'd':
  1444.     {
  1445.       char *p = argv[i] + 2;
  1446.       char c;
  1447.       while (c = *p++) {
  1448.         /* Arg to -d specifies what parts of macros to dump */
  1449.         switch (c) {
  1450.         case 'M':
  1451.           dump_macros = dump_only;
  1452.           no_output = 1;
  1453.           break;
  1454.         case 'N':
  1455.           dump_macros = dump_names;
  1456.           break;
  1457.         case 'D':
  1458.           dump_macros = dump_definitions;
  1459.           break;
  1460.         }
  1461.       }
  1462.     }
  1463.     break;
  1464.  
  1465.       case 'g':
  1466.     if (argv[i][2] == '3')
  1467.       debug_output = 1;
  1468.     break;
  1469.  
  1470.       case 'v':
  1471.     fprintf (stderr, "GNU CPP version %s", version_string);
  1472. #ifdef TARGET_VERSION
  1473.     TARGET_VERSION;
  1474. #endif
  1475.     fprintf (stderr, "\n");
  1476.     verbose = 1;
  1477.     break;
  1478.  
  1479.       case 'H':
  1480.     print_include_names = 1;
  1481.     break;
  1482.  
  1483.       case 'D':
  1484.     if (argv[i][2] != 0)
  1485.       pend_defs[i] = argv[i] + 2;
  1486.     else if (i + 1 == argc)
  1487.       fatal ("Macro name missing after -D option");
  1488.     else
  1489.       i++, pend_defs[i] = argv[i];
  1490.     break;
  1491.  
  1492.       case 'A':
  1493.     {
  1494.       char *p;
  1495.  
  1496.       if (argv[i][2] != 0)
  1497.         p = argv[i] + 2;
  1498.       else if (i + 1 == argc)
  1499.         fatal ("Assertion missing after -A option");
  1500.       else
  1501.         p = argv[++i];
  1502.  
  1503.       if (!strcmp (p, "-")) {
  1504.         /* -A- eliminates all predefined macros and assertions.
  1505.            Let's include also any that were specified earlier
  1506.            on the command line.  That way we can get rid of any
  1507.            that were passed automatically in from GCC.  */
  1508.         int j;
  1509.         inhibit_predefs = 1;
  1510.         for (j = 0; j < i; j++)
  1511.           pend_defs[j] = pend_assertions[j] = 0;
  1512.       } else {
  1513.         pend_assertions[i] = p;
  1514.         pend_assertion_options[i] = "-A";
  1515.       }
  1516.     }
  1517.     break;
  1518.  
  1519.       case 'U':        /* JF #undef something */
  1520.     if (argv[i][2] != 0)
  1521.       pend_undefs[i] = argv[i] + 2;
  1522.     else if (i + 1 == argc)
  1523.       fatal ("Macro name missing after -U option");
  1524.     else
  1525.       pend_undefs[i] = argv[i+1], i++;
  1526.     break;
  1527.  
  1528.       case 'C':
  1529.     put_out_comments = 1;
  1530.     break;
  1531.  
  1532.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1533.     break;
  1534.  
  1535.       case 'P':
  1536.     no_line_commands = 1;
  1537.     break;
  1538.  
  1539.       case '$':            /* Don't include $ in identifiers.  */
  1540.     dollars_in_ident = 0;
  1541.     break;
  1542.  
  1543.       case 'I':            /* Add directory to path for includes.  */
  1544.     {
  1545.       struct file_name_list *dirtmp;
  1546.  
  1547.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1548.         ignore_srcdir = 1;
  1549.         /* Don't use any preceding -I directories for #include <...>.  */
  1550.         first_bracket_include = 0;
  1551.       }
  1552.       else {
  1553.         dirtmp = (struct file_name_list *)
  1554.           xmalloc (sizeof (struct file_name_list));
  1555.         dirtmp->next = 0;        /* New one goes on the end */
  1556.         dirtmp->control_macro = 0;
  1557.         dirtmp->c_system_include_path = 0;
  1558.         if (argv[i][2] != 0)
  1559.           dirtmp->fname = argv[i] + 2;
  1560.         else if (i + 1 == argc)
  1561.           fatal ("Directory name missing after -I option");
  1562.         else
  1563.           dirtmp->fname = argv[++i];
  1564.         dirtmp->got_name_map = 0;
  1565.         append_include_chain (dirtmp, dirtmp);
  1566.       }
  1567.     }
  1568.     break;
  1569.  
  1570.       case 'n':
  1571.     if (!strcmp (argv[i], "-nostdinc"))
  1572.       /* -nostdinc causes no default include directories.
  1573.          You must specify all include-file directories with -I.  */
  1574.       no_standard_includes = 1;
  1575.     else if (!strcmp (argv[i], "-nostdinc++"))
  1576.       /* -nostdinc++ causes no default C++-specific include directories. */
  1577.       no_standard_cplusplus_includes = 1;
  1578.     else if (!strcmp (argv[i], "-noprecomp"))
  1579.       no_precomp = 1;
  1580.     break;
  1581.  
  1582.       case 'u':
  1583.     /* Sun compiler passes undocumented switch "-undef".
  1584.        Let's assume it means to inhibit the predefined symbols.  */
  1585.     inhibit_predefs = 1;
  1586.     break;
  1587.  
  1588.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  1589.     if (in_fname == NULL) {
  1590.       in_fname = "";
  1591.       break;
  1592.     } else if (out_fname == NULL) {
  1593.       out_fname = "";
  1594.       break;
  1595.     }    /* else fall through into error */
  1596.  
  1597.       default:
  1598.     fatal ("Invalid option `%s'", argv[i]);
  1599.       }
  1600.     }
  1601.   }
  1602.  
  1603.   /* Add dirs from CPATH after dirs from -I.  */
  1604.   /* There seems to be confusion about what CPATH should do,
  1605.      so for the moment it is not documented.  */
  1606.   /* Some people say that CPATH should replace the standard include dirs,
  1607.      but that seems pointless: it comes before them, so it overrides them
  1608.      anyway.  */
  1609.   p = (char *) getenv ("CPATH");
  1610.   if (p != 0 && ! no_standard_includes)
  1611.     path_include (p);
  1612.  
  1613.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  1614.   initialize_char_syntax ();
  1615.  
  1616.   /* Initialize output buffer */
  1617.  
  1618.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  1619.   outbuf.bufp = outbuf.buf;
  1620.   outbuf.length = OUTBUF_SIZE;
  1621.  
  1622.   /* Do partial setup of input buffer for the sake of generating
  1623.      early #line directives (when -g is in effect).  */
  1624.  
  1625.   fp = &instack[++indepth];
  1626.   if (in_fname == NULL)
  1627.     in_fname = "";
  1628.   fp->nominal_fname = fp->fname = in_fname;
  1629.   fp->lineno = 0;
  1630.  
  1631.   /* In C++, wchar_t is a distinct basic type, and we can expect
  1632.      __wchar_t to be defined by cc1plus.  */
  1633.   if (cplusplus)
  1634.     wchar_type = "__wchar_t";
  1635.  
  1636.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  1637.      and option processing.  */
  1638.   initialize_builtins (fp, &outbuf);
  1639.  
  1640.   /* Do standard #defines and assertions
  1641.      that identify system and machine type.  */
  1642.  
  1643.   if (!inhibit_predefs) {
  1644.     char *p = (char *) alloca (strlen (predefs) + 1);
  1645.     strcpy (p, predefs);
  1646.     while (*p) {
  1647.       char *q;
  1648.       while (*p == ' ' || *p == '\t')
  1649.     p++;
  1650.       /* Handle -D options.  */ 
  1651.       if (p[0] == '-' && p[1] == 'D') {
  1652.     q = &p[2];
  1653.     while (*p && *p != ' ' && *p != '\t')
  1654.       p++;
  1655.     if (*p != 0)
  1656.       *p++= 0;
  1657.     if (debug_output)
  1658.       output_line_command (fp, &outbuf, 0, same_file);
  1659.     make_definition (q, &outbuf);
  1660.     while (*p == ' ' || *p == '\t')
  1661.       p++;
  1662.       } else if (p[0] == '-' && p[1] == 'A') {
  1663.     /* Handle -A options (assertions).  */ 
  1664.     char *assertion;
  1665.     char *past_name;
  1666.     char *value;
  1667.     char *past_value;
  1668.     char *termination;
  1669.     int save_char;
  1670.  
  1671.     assertion = &p[2];
  1672.     past_name = assertion;
  1673.     /* Locate end of name.  */
  1674.     while (*past_name && *past_name != ' '
  1675.            && *past_name != '\t' && *past_name != '(')
  1676.       past_name++;
  1677.     /* Locate `(' at start of value.  */
  1678.     value = past_name;
  1679.     while (*value && (*value == ' ' || *value == '\t'))
  1680.       value++;
  1681.     if (*value++ != '(')
  1682.       abort ();
  1683.     while (*value && (*value == ' ' || *value == '\t'))
  1684.       value++;
  1685.     past_value = value;
  1686.     /* Locate end of value.  */
  1687.     while (*past_value && *past_value != ' '
  1688.            && *past_value != '\t' && *past_value != ')')
  1689.       past_value++;
  1690.     termination = past_value;
  1691.     while (*termination && (*termination == ' ' || *termination == '\t'))
  1692.       termination++;
  1693.     if (*termination++ != ')')
  1694.       abort ();
  1695.     if (*termination && *termination != ' ' && *termination != '\t')
  1696.       abort ();
  1697.     /* Temporarily null-terminate the value.  */
  1698.     save_char = *termination;
  1699.     *termination = '\0';
  1700.     /* Install the assertion.  */
  1701.     make_assertion ("-A", assertion);
  1702.     *termination = (char) save_char;
  1703.     p = termination;
  1704.     while (*p == ' ' || *p == '\t')
  1705.       p++;
  1706.       } else {
  1707.     abort ();
  1708.       }
  1709.     }
  1710.   }
  1711.  
  1712.   /* Now handle the command line options.  */
  1713.  
  1714.   /* Do -U's, -D's and -A's in the order they were seen.  */
  1715.   for (i = 1; i < argc; i++) {
  1716.     if (pend_undefs[i]) {
  1717.       if (debug_output)
  1718.         output_line_command (fp, &outbuf, 0, same_file);
  1719.       make_undef (pend_undefs[i], &outbuf);
  1720.     }
  1721.     if (pend_defs[i]) {
  1722.       if (debug_output)
  1723.         output_line_command (fp, &outbuf, 0, same_file);
  1724.       make_definition (pend_defs[i], &outbuf);
  1725.     }
  1726.     if (pend_assertions[i])
  1727.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  1728.   }
  1729.  
  1730.   done_initializing = 1;
  1731.  
  1732.   { /* read the appropriate environment variable and if it exists
  1733.        replace include_defaults with the listed path. */
  1734.     char *epath = 0;
  1735.     switch ((objc << 1) + cplusplus)
  1736.       {
  1737.       case 0:
  1738.     epath = getenv ("C_INCLUDE_PATH");
  1739.     break;
  1740.       case 1:
  1741.     epath = getenv ("CPLUS_INCLUDE_PATH");
  1742.     break;
  1743.       case 2:
  1744.     epath = getenv ("OBJC_INCLUDE_PATH");
  1745.     break;
  1746.       case 3:
  1747.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  1748.     break;
  1749.       }
  1750.     /* If the environment var for this language is set,
  1751.        add to the default list of include directories.  */
  1752.     if (epath) {
  1753.       char *nstore = (char *) alloca (strlen (epath) + 2);
  1754.       int num_dirs;
  1755.       char *startp, *endp;
  1756.  
  1757.       for (num_dirs = 1, startp = epath; *startp; startp++)
  1758.     if (*startp == PATH_SEPARATOR)
  1759.       num_dirs++;
  1760.       include_defaults
  1761.     = (struct default_include *) xmalloc ((num_dirs
  1762.                            * sizeof (struct default_include))
  1763.                           + sizeof (include_defaults_array));
  1764.       startp = endp = epath;
  1765.       num_dirs = 0;
  1766.       while (1) {
  1767.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  1768.         if ((*endp == PATH_SEPARATOR
  1769. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  1770. #ifdef __MSDOS__
  1771.          && (endp-startp != 1 || !isalpha (*startp))
  1772. #endif
  1773. #endif
  1774.          )
  1775.             || *endp == 0) {
  1776.       strncpy (nstore, startp, endp-startp);
  1777.       if (endp == startp)
  1778.         strcpy (nstore, ".");
  1779.       else
  1780.         nstore[endp-startp] = '\0';
  1781.  
  1782.       include_defaults[num_dirs].fname = savestring (nstore);
  1783.       include_defaults[num_dirs].cplusplus = cplusplus;
  1784.       include_defaults[num_dirs].cxx_aware = 1;
  1785.       num_dirs++;
  1786.       if (*endp == '\0')
  1787.         break;
  1788.       endp = startp = endp + 1;
  1789.     } else
  1790.       endp++;
  1791.       }
  1792.       /* Put the usual defaults back in at the end.  */
  1793.       bcopy ((char *) include_defaults_array,
  1794.          (char *) &include_defaults[num_dirs],
  1795.          sizeof (include_defaults_array));
  1796.     }
  1797.   }
  1798.  
  1799.   append_include_chain (before_system, last_before_system);
  1800.   first_system_include = before_system;
  1801.  
  1802.   /* Unless -fnostdinc,
  1803.      tack on the standard include file dirs to the specified list */
  1804.   if (!no_standard_includes) {
  1805.     struct default_include *p = include_defaults;
  1806.     char *specd_prefix = include_prefix;
  1807.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  1808.     int default_len = 0;
  1809.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1810.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  1811.       default_len = strlen (default_prefix) - 7;
  1812.       default_prefix[default_len] = 0;
  1813.     }
  1814.     /* Search "translated" versions of GNU directories.
  1815.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  1816.     if (specd_prefix != 0 && default_len != 0)
  1817.       for (p = include_defaults; p->fname; p++) {
  1818.     /* Some standard dirs are only for C++.  */
  1819.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  1820.       /* Does this dir start with the prefix?  */
  1821.       if (!strncmp (p->fname, default_prefix, default_len)) {
  1822.         /* Yes; change prefix and add to search list.  */
  1823.         struct file_name_list *new
  1824.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  1825.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  1826.         char *str = (char *) xmalloc (this_len + 1);
  1827.         strcpy (str, specd_prefix);
  1828.         strcat (str, p->fname + default_len);
  1829.         new->fname = str;
  1830.         new->control_macro = 0;
  1831.         new->c_system_include_path = !p->cxx_aware;
  1832.         new->got_name_map = 0;
  1833.         append_include_chain (new, new);
  1834.         if (first_system_include == 0)
  1835.           first_system_include = new;
  1836.       }
  1837.     }
  1838.       }
  1839.     /* Search ordinary names for GNU include directories.  */
  1840.     for (p = include_defaults; p->fname; p++) {
  1841.       /* Some standard dirs are only for C++.  */
  1842.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  1843.     struct file_name_list *new
  1844.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  1845.     new->control_macro = 0;
  1846.     new->c_system_include_path = !p->cxx_aware;
  1847.     new->fname = p->fname;
  1848.     new->got_name_map = 0;
  1849.     append_include_chain (new, new);
  1850.     if (first_system_include == 0)
  1851.       first_system_include = new;
  1852.       }
  1853.     }
  1854.   }
  1855.  
  1856.   /* Tack the after_include chain at the end of the include chain.  */
  1857.   append_include_chain (after_include, last_after_include);
  1858.   if (first_system_include == 0)
  1859.     first_system_include = after_include;
  1860.  
  1861.   /* With -v, print the list of dirs to search.  */
  1862.   if (verbose) {
  1863.     struct file_name_list *p;
  1864.     fprintf (stderr, "#include \"...\" search starts here:\n");
  1865.     for (p = include; p; p = p->next) {
  1866.       if (p == first_bracket_include)
  1867.     fprintf (stderr, "#include <...> search starts here:\n");
  1868.       fprintf (stderr, " %s\n", p->fname);
  1869.     }
  1870.     fprintf (stderr, "End of search list.\n");
  1871.   }
  1872.  
  1873.   /* Scan the -imacros files before the main input.
  1874.      Much like #including them, but with no_output set
  1875.      so that only their macro definitions matter.  */
  1876.  
  1877.   no_output++; no_record_file++;
  1878.   for (i = 1; i < argc; i++)
  1879.     if (pend_files[i]) {
  1880.       int fd = open (pend_files[i], O_RDONLY, 0666);
  1881.       if (fd < 0) {
  1882.     perror_with_name (pend_files[i]);
  1883.     return FAILURE_EXIT_CODE;
  1884.       }
  1885.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  1886.     }
  1887.   no_output--; no_record_file--;
  1888.  
  1889.   /* Copy the entire contents of the main input file into
  1890.      the stacked input buffer previously allocated for it.  */
  1891.  
  1892.   /* JF check for stdin */
  1893.   if (in_fname == NULL || *in_fname == 0) {
  1894.     in_fname = "";
  1895.     f = 0;
  1896.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  1897.     goto perror;
  1898.  
  1899.   /* -MG doesn't select the form of output and must be specified with one of
  1900.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  1901.      inhibit compilation.  */
  1902.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  1903.     fatal ("-MG must be specified with one of -M or -MM");
  1904.  
  1905.   /* Either of two environment variables can specify output of deps.
  1906.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  1907.      where OUTPUT_FILE is the file to write deps info to
  1908.      and DEPS_TARGET is the target to mention in the deps.  */
  1909.  
  1910.   if (print_deps == 0
  1911.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  1912.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  1913.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  1914.     char *s;
  1915.     char *output_file;
  1916.  
  1917.     if (spec == 0) {
  1918.       spec = getenv ("SUNPRO_DEPENDENCIES");
  1919.       print_deps = 2;
  1920.     }
  1921.     else
  1922.       print_deps = 1;
  1923.  
  1924.     s = spec;
  1925.     /* Find the space before the DEPS_TARGET, if there is one.  */
  1926.     /* This should use index.  (mrs) */
  1927.     while (*s != 0 && *s != ' ') s++;
  1928.     if (*s != 0) {
  1929.       deps_target = s + 1;
  1930.       output_file = (char *) xmalloc (s - spec + 1);
  1931.       bcopy (spec, output_file, s - spec);
  1932.       output_file[s - spec] = 0;
  1933.     }
  1934.     else {
  1935.       deps_target = 0;
  1936.       output_file = spec;
  1937.     }
  1938.       
  1939.     deps_file = output_file;
  1940.     deps_mode = "a";
  1941.   }
  1942.  
  1943.   /* For -M, print the expected object file name
  1944.      as the target of this Make-rule.  */
  1945.   if (print_deps) {
  1946.     deps_allocated_size = 200;
  1947.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  1948.     deps_buffer[0] = 0;
  1949.     deps_size = 0;
  1950.     deps_column = 0;
  1951.  
  1952.     if (deps_target) {
  1953.       deps_output (deps_target, ':');
  1954.     } else if (*in_fname == 0) {
  1955.       deps_output ("-", ':');
  1956.     } else {
  1957.       char *p, *q;
  1958.       int len;
  1959.  
  1960.       /* Discard all directory prefixes from filename.  */
  1961.       if ((q = rindex (in_fname, '/')) != NULL)
  1962.     ++q;
  1963.       else
  1964.     q = in_fname;
  1965.  
  1966.       /* Copy remainder to mungable area.  */
  1967.       p = (char *) alloca (strlen(q) + 8);
  1968.       strcpy (p, q);
  1969.  
  1970.       /* Output P, but remove known suffixes.  */
  1971.       len = strlen (p);
  1972.       q = p + len;
  1973.       if (len >= 2
  1974.       && p[len - 2] == '.'
  1975.       && index("cCsSm", p[len - 1]))
  1976.     q = p + (len - 2);
  1977.       else if (len >= 3
  1978.            && p[len - 3] == '.'
  1979.            && p[len - 2] == 'c'
  1980.            && p[len - 1] == 'c')
  1981.     q = p + (len - 3);
  1982.       else if (len >= 4
  1983.            && p[len - 4] == '.'
  1984.            && p[len - 3] == 'c'
  1985.            && p[len - 2] == 'x'
  1986.            && p[len - 1] == 'x')
  1987.     q = p + (len - 4);
  1988.       else if (len >= 4
  1989.            && p[len - 4] == '.'
  1990.            && p[len - 3] == 'c'
  1991.            && p[len - 2] == 'p'
  1992.            && p[len - 1] == 'p')
  1993.     q = p + (len - 4);
  1994.  
  1995.       /* Supply our own suffix.  */
  1996. #ifndef VMS
  1997.       strcpy (q, ".o");
  1998. #else
  1999.       strcpy (q, ".obj");
  2000. #endif
  2001.  
  2002.       deps_output (p, ':');
  2003.       deps_output (in_fname, ' ');
  2004.     }
  2005.   }
  2006.  
  2007.   file_size_and_mode (f, &st_mode, &st_size);
  2008.   fp->nominal_fname = fp->fname = in_fname;
  2009.   fp->lineno = 1;
  2010.   fp->system_header_p = 0;
  2011.   /* JF all this is mine about reading pipes and ttys */
  2012.   if (! S_ISREG (st_mode)) {
  2013.     /* Read input from a file that is not a normal disk file.
  2014.        We cannot preallocate a buffer with the correct size,
  2015.        so we must read in the file a piece at the time and make it bigger.  */
  2016.     int size;
  2017.     int bsize;
  2018.     int cnt;
  2019.  
  2020.     bsize = 2000;
  2021.     size = 0;
  2022.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  2023.     for (;;) {
  2024.       cnt = safe_read (f, fp->buf + size, bsize - size);
  2025.       if (cnt < 0) goto perror;    /* error! */
  2026.       size += cnt;
  2027.       if (size != bsize) break;    /* End of file */
  2028.       bsize *= 2;
  2029.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  2030.     }
  2031.     fp->length = size;
  2032.   } else {
  2033.     /* Read a file whose size we can determine in advance.
  2034.        For the sake of VMS, st_size is just an upper bound.  */
  2035.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  2036.     fp->length = safe_read (f, fp->buf, st_size);
  2037.     if (fp->length < 0) goto perror;
  2038.   }
  2039.   fp->bufp = fp->buf;
  2040.   fp->if_stack = if_stack;
  2041.  
  2042.   /* Make sure data ends with a newline.  And put a null after it.  */
  2043.  
  2044.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  2045.       /* Backslash-newline at end is not good enough.  */
  2046.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  2047.     fp->buf[fp->length++] = '\n';
  2048.     missing_newline = 1;
  2049.   }
  2050.   fp->buf[fp->length] = '\0';
  2051.  
  2052.   /* Unless inhibited, convert trigraphs in the input.  */
  2053.  
  2054.   if (!no_trigraphs)
  2055.     trigraph_pcp (fp);
  2056.  
  2057.   /* Now that we know the input file is valid, open the output.  */
  2058.  
  2059.   if (!out_fname || !strcmp (out_fname, ""))
  2060.     out_fname = "stdout";
  2061.   else if (! freopen (out_fname, "w", stdout))
  2062.     pfatal_with_name (out_fname);
  2063.  
  2064.   output_line_command (fp, &outbuf, 0, same_file);
  2065.  
  2066.   /* Scan the -include files before the main input.  */
  2067.  
  2068.   no_record_file++;
  2069.   for (i = 1; i < argc; i++)
  2070.     if (pend_includes[i]) {
  2071.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  2072.       if (fd < 0) {
  2073.     perror_with_name (pend_includes[i]);
  2074.     return FAILURE_EXIT_CODE;
  2075.       }
  2076.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  2077.     }
  2078.   no_record_file--;
  2079.  
  2080.   /* Scan the input, processing macros and directives.  */
  2081.  
  2082.   rescan (&outbuf, 0);
  2083.  
  2084.   if (missing_newline)
  2085.     fp->lineno--;
  2086.  
  2087.   if (pedantic && missing_newline)
  2088.     pedwarn ("file does not end in newline");
  2089.  
  2090.   /* Now we have processed the entire input
  2091.      Write whichever kind of output has been requested.  */
  2092.  
  2093.   if (dump_macros == dump_only)
  2094.     dump_all_macros ();
  2095.   else if (! inhibit_output) {
  2096.     write_output ();
  2097.   }
  2098.  
  2099.   if (print_deps) {
  2100.     /* Don't actually write the deps file if compilation has failed.  */
  2101.     if (errors == 0) {
  2102.       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
  2103.     pfatal_with_name (deps_file);
  2104.       fputs (deps_buffer, deps_stream);
  2105.       putc ('\n', deps_stream);
  2106.       if (deps_file) {
  2107.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  2108.       fatal ("I/O error on output");
  2109.       }
  2110.     }
  2111.   }
  2112.  
  2113.   if (pcp_outfile && pcp_outfile != stdout
  2114.       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
  2115.     fatal ("I/O error on `-pcp' output");
  2116.  
  2117.   if (ferror (stdout) || fclose (stdout) != 0)
  2118.     fatal ("I/O error on output");
  2119.  
  2120.   if (errors)
  2121.     exit (FAILURE_EXIT_CODE);
  2122.   exit (SUCCESS_EXIT_CODE);
  2123.  
  2124.  perror:
  2125.   pfatal_with_name (in_fname);
  2126.   return 0;
  2127. }
  2128.  
  2129. /* Given a colon-separated list of file names PATH,
  2130.    add all the names to the search path for include files.  */
  2131.  
  2132. static void
  2133. path_include (path)
  2134.      char *path;
  2135. {
  2136.   char *p;
  2137.  
  2138.   p = path;
  2139.  
  2140.   if (*p)
  2141.     while (1) {
  2142.       char *q = p;
  2143.       char *name;
  2144.       struct file_name_list *dirtmp;
  2145.  
  2146.       /* Find the end of this name.  */
  2147.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  2148.       if (p == q) {
  2149.     /* An empty name in the path stands for the current directory.  */
  2150.     name = (char *) xmalloc (2);
  2151.     name[0] = '.';
  2152.     name[1] = 0;
  2153.       } else {
  2154.     /* Otherwise use the directory that is named.  */
  2155.     name = (char *) xmalloc (q - p + 1);
  2156.     bcopy (p, name, q - p);
  2157.     name[q - p] = 0;
  2158.       }
  2159.  
  2160.       dirtmp = (struct file_name_list *)
  2161.     xmalloc (sizeof (struct file_name_list));
  2162.       dirtmp->next = 0;        /* New one goes on the end */
  2163.       dirtmp->control_macro = 0;
  2164.       dirtmp->c_system_include_path = 0;
  2165.       dirtmp->fname = name;
  2166.       dirtmp->got_name_map = 0;
  2167.       append_include_chain (dirtmp, dirtmp);
  2168.  
  2169.       /* Advance past this name.  */
  2170.       p = q;
  2171.       if (*p == 0)
  2172.     break;
  2173.       /* Skip the colon.  */
  2174.       p++;
  2175.     }
  2176. }
  2177.  
  2178. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  2179.    before main CCCP processing.  Name `pcp' is also in honor of the
  2180.    drugs the trigraph designers must have been on.
  2181.  
  2182.    Using an extra pass through the buffer takes a little extra time,
  2183.    but is infinitely less hairy than trying to handle trigraphs inside
  2184.    strings, etc. everywhere, and also makes sure that trigraphs are
  2185.    only translated in the top level of processing. */
  2186.  
  2187. static void
  2188. trigraph_pcp (buf)
  2189.      FILE_BUF *buf;
  2190. {
  2191.   register U_CHAR c, *fptr, *bptr, *sptr;
  2192.   int len;
  2193.  
  2194.   fptr = bptr = sptr = buf->buf;
  2195.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  2196.     if (*++sptr != '?')
  2197.       continue;
  2198.     switch (*++sptr) {
  2199.       case '=':
  2200.       c = '#';
  2201.       break;
  2202.     case '(':
  2203.       c = '[';
  2204.       break;
  2205.     case '/':
  2206.       c = '\\';
  2207.       break;
  2208.     case ')':
  2209.       c = ']';
  2210.       break;
  2211.     case '\'':
  2212.       c = '^';
  2213.       break;
  2214.     case '<':
  2215.       c = '{';
  2216.       break;
  2217.     case '!':
  2218.       c = '|';
  2219.       break;
  2220.     case '>':
  2221.       c = '}';
  2222.       break;
  2223.     case '-':
  2224.       c  = '~';
  2225.       break;
  2226.     case '?':
  2227.       sptr--;
  2228.       continue;
  2229.     default:
  2230.       continue;
  2231.     }
  2232.     len = sptr - fptr - 2;
  2233.  
  2234.     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
  2235.        C, this will be memmove (). */
  2236.     if (bptr != fptr && len > 0)
  2237.       bcopy ((char *) fptr, (char *) bptr, len);
  2238.  
  2239.     bptr += len;
  2240.     *bptr++ = c;
  2241.     fptr = ++sptr;
  2242.   }
  2243.   len = buf->length - (fptr - buf->buf);
  2244.   if (bptr != fptr && len > 0)
  2245.     bcopy ((char *) fptr, (char *) bptr, len);
  2246.   buf->length -= fptr - bptr;
  2247.   buf->buf[buf->length] = '\0';
  2248.   if (warn_trigraphs && fptr != bptr)
  2249.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  2250. }
  2251.  
  2252. /* Move all backslash-newline pairs out of embarrassing places.
  2253.    Exchange all such pairs following BP
  2254.    with any potentially-embarrassing characters that follow them.
  2255.    Potentially-embarrassing characters are / and *
  2256.    (because a backslash-newline inside a comment delimiter
  2257.    would cause it not to be recognized).  */
  2258.  
  2259. static void
  2260. newline_fix (bp)
  2261.      U_CHAR *bp;
  2262. {
  2263.   register U_CHAR *p = bp;
  2264.   register int count = 0;
  2265.  
  2266.   /* First count the backslash-newline pairs here.  */
  2267.  
  2268.   while (1) {
  2269.     if (p[0] == '\\') {
  2270.       if (p[1] == '\n')
  2271.     p += 2, count++;
  2272.       else if (p[1] == '\r' && p[2] == '\n')
  2273.     p += 3, count++;
  2274.       else
  2275.     break;
  2276.     } else
  2277.       break;
  2278.   }
  2279.  
  2280.   /* What follows the backslash-newlines is not embarrassing.  */
  2281.  
  2282.   if (count == 0 || (*p != '/' && *p != '*'))
  2283.     return;
  2284.  
  2285.   /* Copy all potentially embarrassing characters
  2286.      that follow the backslash-newline pairs
  2287.      down to where the pairs originally started.  */
  2288.  
  2289.   while (*p == '*' || *p == '/')
  2290.     *bp++ = *p++;
  2291.  
  2292.   /* Now write the same number of pairs after the embarrassing chars.  */
  2293.   while (count-- > 0) {
  2294.     *bp++ = '\\';
  2295.     *bp++ = '\n';
  2296.   }
  2297. }
  2298.  
  2299. /* Like newline_fix but for use within a directive-name.
  2300.    Move any backslash-newlines up past any following symbol constituents.  */
  2301.  
  2302. static void
  2303. name_newline_fix (bp)
  2304.      U_CHAR *bp;
  2305. {
  2306.   register U_CHAR *p = bp;
  2307.   register int count = 0;
  2308.  
  2309.   /* First count the backslash-newline pairs here.  */
  2310.   while (1) {
  2311.     if (p[0] == '\\') {
  2312.       if (p[1] == '\n')
  2313.     p += 2, count++;
  2314.       else if (p[1] == '\r' && p[2] == '\n')
  2315.     p += 3, count++;
  2316.       else
  2317.     break;
  2318.     } else
  2319.       break;
  2320.   }
  2321.  
  2322.   /* What follows the backslash-newlines is not embarrassing.  */
  2323.  
  2324.   if (count == 0 || !is_idchar[*p])
  2325.     return;
  2326.  
  2327.   /* Copy all potentially embarrassing characters
  2328.      that follow the backslash-newline pairs
  2329.      down to where the pairs originally started.  */
  2330.  
  2331.   while (is_idchar[*p])
  2332.     *bp++ = *p++;
  2333.  
  2334.   /* Now write the same number of pairs after the embarrassing chars.  */
  2335.   while (count-- > 0) {
  2336.     *bp++ = '\\';
  2337.     *bp++ = '\n';
  2338.   }
  2339. }
  2340.  
  2341. /* Look for lint commands in comments.
  2342.  
  2343.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2344.    scan within the comment -- it should start, after lwsp, with a lint command.
  2345.    If so that command is returned as a (constant) string.
  2346.  
  2347.    Upon return, any arg will be pointed to with argstart and will be
  2348.    arglen long.  Note that we don't parse that arg since it will just
  2349.    be printed out again.
  2350. */
  2351.  
  2352. static char *
  2353. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2354.      register U_CHAR *ibp;
  2355.      register U_CHAR *limit;
  2356.      U_CHAR **argstart;        /* point to command arg */
  2357.      int *arglen, *cmdlen;    /* how long they are */
  2358. {
  2359.   long linsize;
  2360.   register U_CHAR *numptr;    /* temp for arg parsing */
  2361.  
  2362.   *arglen = 0;
  2363.  
  2364.   SKIP_WHITE_SPACE (ibp);
  2365.  
  2366.   if (ibp >= limit) return NULL;
  2367.  
  2368.   linsize = limit - ibp;
  2369.   
  2370.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2371.   if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
  2372.     *cmdlen = 10;
  2373.     return "NOTREACHED";
  2374.   }
  2375.   if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
  2376.     *cmdlen = 8;
  2377.     return "ARGSUSED";
  2378.   }
  2379.   if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 11)) {
  2380.     *cmdlen = 11;
  2381.     return "LINTLIBRARY";
  2382.   }
  2383.   if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
  2384.     *cmdlen = 7;
  2385.     ibp += 7; linsize -= 7;
  2386.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  2387.  
  2388.     /* OK, read a number */
  2389.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  2390.      numptr++);
  2391.     *arglen = numptr - *argstart;
  2392.     return "VARARGS";
  2393.   }
  2394.   return NULL;
  2395. }
  2396.  
  2397. /*
  2398.  * The main loop of the program.
  2399.  *
  2400.  * Read characters from the input stack, transferring them to the
  2401.  * output buffer OP.
  2402.  *
  2403.  * Macros are expanded and push levels on the input stack.
  2404.  * At the end of such a level it is popped off and we keep reading.
  2405.  * At the end of any other kind of level, we return.
  2406.  * #-directives are handled, except within macros.
  2407.  *
  2408.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  2409.  * and insert them when appropriate.  This is set while scanning macro
  2410.  * arguments before substitution.  It is zero when scanning for final output.
  2411.  *   There are three types of Newline markers:
  2412.  *   * Newline -  follows a macro name that was not expanded
  2413.  *     because it appeared inside an expansion of the same macro.
  2414.  *     This marker prevents future expansion of that identifier.
  2415.  *     When the input is rescanned into the final output, these are deleted.
  2416.  *     These are also deleted by ## concatenation.
  2417.  *   * Newline Space (or Newline and any other whitespace character)
  2418.  *     stands for a place that tokens must be separated or whitespace
  2419.  *     is otherwise desirable, but where the ANSI standard specifies there
  2420.  *     is no whitespace.  This marker turns into a Space (or whichever other
  2421.  *     whitespace char appears in the marker) in the final output,
  2422.  *     but it turns into nothing in an argument that is stringified with #.
  2423.  *     Such stringified arguments are the only place where the ANSI standard
  2424.  *     specifies with precision that whitespace may not appear.
  2425.  *
  2426.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  2427.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  2428.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  2429.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  2430.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  2431.  */
  2432.  
  2433. static void
  2434. rescan (op, output_marks)
  2435.      FILE_BUF *op;
  2436.      int output_marks;
  2437. {
  2438.   /* Character being scanned in main loop.  */
  2439.   register U_CHAR c;
  2440.  
  2441.   /* Length of pending accumulated identifier.  */
  2442.   register int ident_length = 0;
  2443.  
  2444.   /* Hash code of pending accumulated identifier.  */
  2445.   register int hash = 0;
  2446.  
  2447.   /* Current input level (&instack[indepth]).  */
  2448.   FILE_BUF *ip;
  2449.  
  2450.   /* Pointer for scanning input.  */
  2451.   register U_CHAR *ibp;
  2452.  
  2453.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  2454.   register U_CHAR *limit;
  2455.  
  2456.   /* Pointer for storing output.  */
  2457.   register U_CHAR *obp;
  2458.  
  2459.   /* REDO_CHAR is nonzero if we are processing an identifier
  2460.      after backing up over the terminating character.
  2461.      Sometimes we process an identifier without backing up over
  2462.      the terminating character, if the terminating character
  2463.      is not special.  Backing up is done so that the terminating character
  2464.      will be dispatched on again once the identifier is dealt with.  */
  2465.   int redo_char = 0;
  2466.  
  2467.   /* 1 if within an identifier inside of which a concatenation
  2468.      marker (Newline -) has been seen.  */
  2469.   int concatenated = 0;
  2470.  
  2471.   /* While scanning a comment or a string constant,
  2472.      this records the line it started on, for error messages.  */
  2473.   int start_line;
  2474.  
  2475.   /* Record position of last `real' newline.  */
  2476.   U_CHAR *beg_of_line;
  2477.  
  2478. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  2479.  
  2480. #define POPMACRO \
  2481. do { ip->macro->type = T_MACRO;        \
  2482.      if (ip->free_ptr) free (ip->free_ptr);    \
  2483.      --indepth; } while (0)
  2484.  
  2485. /* Reload `rescan's local variables that describe the current
  2486.    level of the input stack.  */
  2487.  
  2488. #define RECACHE  \
  2489. do { ip = &instack[indepth];        \
  2490.      ibp = ip->bufp;            \
  2491.      limit = ip->buf + ip->length;    \
  2492.      op->bufp = obp;            \
  2493.      check_expand (op, limit - ibp);    \
  2494.      beg_of_line = 0;            \
  2495.      obp = op->bufp; } while (0)
  2496.  
  2497.   if (no_output && instack[indepth].fname != 0)
  2498.     skip_if_group (&instack[indepth], 1);
  2499.  
  2500.   obp = op->bufp;
  2501.   RECACHE;
  2502.  
  2503.   beg_of_line = ibp;
  2504.  
  2505.   /* Our caller must always put a null after the end of
  2506.      the input at each input stack level.  */
  2507.   if (*limit != 0)
  2508.     abort ();
  2509.  
  2510.   while (1) {
  2511.     c = *ibp++;
  2512.     *obp++ = c;
  2513.  
  2514.     switch (c) {
  2515.     case '\\':
  2516.       if (ibp >= limit)
  2517.     break;
  2518.       if (*ibp == '\n') {
  2519.     /* Always merge lines ending with backslash-newline,
  2520.        even in middle of identifier.  */
  2521.     ++ibp;
  2522.     ++ip->lineno;
  2523.     --obp;        /* remove backslash from obuf */
  2524.     break;
  2525.       }
  2526.       /* Otherwise, backslash suppresses specialness of following char,
  2527.      so copy it here to prevent the switch from seeing it.
  2528.      But first get any pending identifier processed.  */
  2529.       if (ident_length > 0)
  2530.     goto specialchar;
  2531.       *obp++ = *ibp++;
  2532.       break;
  2533.  
  2534.     case '#':
  2535.       if (assertions_flag) {
  2536.     /* Copy #foo (bar lose) without macro expansion.  */
  2537.     SKIP_WHITE_SPACE (ibp);
  2538.     while (is_idchar[*ibp])
  2539.       *obp++ = *ibp++;
  2540.     SKIP_WHITE_SPACE (ibp);
  2541.     if (*ibp == '(') {
  2542.       ip->bufp = ibp;
  2543.       skip_paren_group (ip);
  2544.       bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
  2545.       obp += ip->bufp - ibp;
  2546.       ibp = ip->bufp;
  2547.     }
  2548.       }
  2549.  
  2550.       /* If this is expanding a macro definition, don't recognize
  2551.      preprocessor directives.  */
  2552.       if (ip->macro != 0)
  2553.     goto randomchar;
  2554.       /* If this is expand_into_temp_buffer, recognize them
  2555.      only after an actual newline at this level,
  2556.      not at the beginning of the input level.  */
  2557.       if (ip->fname == 0 && beg_of_line == ip->buf)
  2558.     goto randomchar;
  2559.       if (ident_length)
  2560.     goto specialchar;
  2561.  
  2562.       
  2563.       /* # keyword: a # must be first nonblank char on the line */
  2564.       if (beg_of_line == 0)
  2565.     goto randomchar;
  2566.       {
  2567.     U_CHAR *bp;
  2568.  
  2569.     /* Scan from start of line, skipping whitespace, comments
  2570.        and backslash-newlines, and see if we reach this #.
  2571.        If not, this # is not special.  */
  2572.     bp = beg_of_line;
  2573.     /* If -traditional, require # to be at beginning of line.  */
  2574.     if (!traditional)
  2575.       while (1) {
  2576.         if (is_hor_space[*bp])
  2577.           bp++;
  2578.         else if (*bp == '\\' && bp[1] == '\n')
  2579.           bp += 2;
  2580.         else if (*bp == '/' && bp[1] == '*') {
  2581.           bp += 2;
  2582.           while (!(*bp == '*' && bp[1] == '/'))
  2583.         bp++;
  2584.           bp += 2;
  2585.         }
  2586.         /* There is no point in trying to deal with C++ // comments here,
  2587.            because if there is one, then this # must be part of the
  2588.            comment and we would never reach here.  */
  2589.         else break;
  2590.       }
  2591.     if (bp + 1 != ibp)
  2592.       goto randomchar;
  2593.       }
  2594.  
  2595.       /* This # can start a directive.  */
  2596.  
  2597.       --obp;        /* Don't copy the '#' */
  2598.  
  2599.       ip->bufp = ibp;
  2600.       op->bufp = obp;
  2601.       if (! handle_directive (ip, op)) {
  2602. #ifdef USE_C_ALLOCA
  2603.     alloca (0);
  2604. #endif
  2605.     /* Not a known directive: treat it as ordinary text.
  2606.        IP, OP, IBP, etc. have not been changed.  */
  2607.     if (no_output && instack[indepth].fname) {
  2608.       /* If not generating expanded output,
  2609.          what we do with ordinary text is skip it.
  2610.          Discard everything until next # directive.  */
  2611.       skip_if_group (&instack[indepth], 1);
  2612.       RECACHE;
  2613.       beg_of_line = ibp;
  2614.       break;
  2615.     }
  2616.     ++obp;        /* Copy the '#' after all */
  2617.     /* Don't expand an identifier that could be a macro directive.
  2618.        (Section 3.8.3 of the ANSI C standard)            */
  2619.     SKIP_WHITE_SPACE (ibp);
  2620.     if (is_idstart[*ibp])
  2621.       {
  2622.         *obp++ = *ibp++;
  2623.         while (is_idchar[*ibp])
  2624.           *obp++ = *ibp++;
  2625.       }
  2626.     goto randomchar;
  2627.       }
  2628. #ifdef USE_C_ALLOCA
  2629.       alloca (0);
  2630. #endif
  2631.       /* A # directive has been successfully processed.  */
  2632.       /* If not generating expanded output, ignore everything until
  2633.      next # directive.  */
  2634.       if (no_output && instack[indepth].fname)
  2635.     skip_if_group (&instack[indepth], 1);
  2636.       obp = op->bufp;
  2637.       RECACHE;
  2638.       beg_of_line = ibp;
  2639.       break;
  2640.  
  2641.     case '\"':            /* skip quoted string */
  2642.     case '\'':
  2643.       /* A single quoted string is treated like a double -- some
  2644.      programs (e.g., troff) are perverse this way */
  2645.  
  2646.       if (ident_length)
  2647.     goto specialchar;
  2648.  
  2649.       start_line = ip->lineno;
  2650.  
  2651.       /* Skip ahead to a matching quote.  */
  2652.  
  2653.       while (1) {
  2654.     if (ibp >= limit) {
  2655.       if (ip->macro != 0) {
  2656.         /* try harder: this string crosses a macro expansion boundary.
  2657.            This can happen naturally if -traditional.
  2658.            Otherwise, only -D can make a macro with an unmatched quote.  */
  2659.         POPMACRO;
  2660.         RECACHE;
  2661.         continue;
  2662.       }
  2663.       if (!traditional) {
  2664.         error_with_line (line_for_error (start_line),
  2665.                  "unterminated string or character constant");
  2666.         error_with_line (multiline_string_line,
  2667.                  "possible real start of unterminated constant");
  2668.         multiline_string_line = 0;
  2669.       }
  2670.       break;
  2671.     }
  2672.     *obp++ = *ibp;
  2673.     switch (*ibp++) {
  2674.     case '\n':
  2675.       ++ip->lineno;
  2676.       ++op->lineno;
  2677.       /* Traditionally, end of line ends a string constant with no error.
  2678.          So exit the loop and record the new line.  */
  2679.       if (traditional) {
  2680.         beg_of_line = ibp;
  2681.         goto while2end;
  2682.       }
  2683.       if (c == '\'') {
  2684.         error_with_line (line_for_error (start_line),
  2685.                  "unterminated character constant");
  2686.         goto while2end;
  2687.       }
  2688.       if (pedantic && multiline_string_line == 0) {
  2689.         pedwarn_with_line (line_for_error (start_line),
  2690.                    "string constant runs past end of line");
  2691.       }
  2692.       if (multiline_string_line == 0)
  2693.         multiline_string_line = ip->lineno - 1;
  2694.       break;
  2695.  
  2696.     case '\\':
  2697.       if (ibp >= limit)
  2698.         break;
  2699.       if (*ibp == '\n') {
  2700.         /* Backslash newline is replaced by nothing at all,
  2701.            but keep the line counts correct.  */
  2702.         --obp;
  2703.         ++ibp;
  2704.         ++ip->lineno;
  2705.       } else {
  2706.         /* ANSI stupidly requires that in \\ the second \
  2707.            is *not* prevented from combining with a newline.  */
  2708.         while (*ibp == '\\' && ibp[1] == '\n') {
  2709.           ibp += 2;
  2710.           ++ip->lineno;
  2711.         }
  2712.         *obp++ = *ibp++;
  2713.       }
  2714.       break;
  2715.  
  2716.     case '\"':
  2717.     case '\'':
  2718.       if (ibp[-1] == c)
  2719.         goto while2end;
  2720.       break;
  2721.     }
  2722.       }
  2723.     while2end:
  2724.       break;
  2725.  
  2726.     case '/':
  2727.       if (*ibp == '\\' && ibp[1] == '\n')
  2728.     newline_fix (ibp);
  2729.  
  2730.       if (*ibp != '*'
  2731.       && !(cplusplus_comments && *ibp == '/'))
  2732.     goto randomchar;
  2733.       if (ip->macro != 0)
  2734.     goto randomchar;
  2735.       if (ident_length)
  2736.     goto specialchar;
  2737.  
  2738.       if (*ibp == '/') {
  2739.     /* C++ style comment... */
  2740.     start_line = ip->lineno;
  2741.  
  2742.     --ibp;            /* Back over the slash */
  2743.     --obp;
  2744.  
  2745.     /* Comments are equivalent to spaces. */
  2746.     if (! put_out_comments)
  2747.       *obp++ = ' ';
  2748.     else {
  2749.       /* must fake up a comment here */
  2750.       *obp++ = '/';
  2751.       *obp++ = '/';
  2752.     }
  2753.     {
  2754.       U_CHAR *before_bp = ibp+2;
  2755.  
  2756.       while (ibp < limit) {
  2757.         if (ibp[-1] != '\\' && *ibp == '\n') {
  2758.           if (put_out_comments) {
  2759.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  2760.         obp += ibp - before_bp;
  2761.           }
  2762.           break;
  2763.         } else {
  2764.           if (*ibp == '\n') {
  2765.         ++ip->lineno;
  2766.         /* Copy the newline into the output buffer, in order to
  2767.            avoid the pain of a #line every time a multiline comment
  2768.            is seen.  */
  2769.         if (!put_out_comments)
  2770.           *obp++ = '\n';
  2771.         ++op->lineno;
  2772.           }
  2773.           ibp++;
  2774.         }
  2775.       }
  2776.       break;
  2777.     }
  2778.       }
  2779.  
  2780.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  2781.  
  2782.       start_line = ip->lineno;
  2783.  
  2784.       ++ibp;            /* Skip the star. */
  2785.  
  2786.       /* If this cpp is for lint, we peek inside the comments: */
  2787.       if (for_lint) {
  2788.     U_CHAR *argbp;
  2789.     int cmdlen, arglen;
  2790.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  2791.  
  2792.     if (lintcmd != NULL) {
  2793.       /* I believe it is always safe to emit this newline: */
  2794.       obp[-1] = '\n';
  2795.       bcopy ("#pragma lint ", (char *) obp, 13);
  2796.       obp += 13;
  2797.       bcopy (lintcmd, (char *) obp, cmdlen);
  2798.       obp += cmdlen;
  2799.  
  2800.       if (arglen != 0) {
  2801.         *(obp++) = ' ';
  2802.         bcopy (argbp, (char *) obp, arglen);
  2803.         obp += arglen;
  2804.       }
  2805.  
  2806.       /* OK, now bring us back to the state we were in before we entered
  2807.          this branch.  We need #line b/c the newline for the pragma
  2808.          could fuck things up. */
  2809.       output_line_command (ip, op, 0, same_file);
  2810.       *(obp++) = ' ';    /* just in case, if comments are copied thru */
  2811.       *(obp++) = '/';
  2812.     }
  2813.       }
  2814.  
  2815.       /* Comments are equivalent to spaces.
  2816.      Note that we already output the slash; we might not want it.
  2817.      For -traditional, a comment is equivalent to nothing.  */
  2818.       if (! put_out_comments) {
  2819.     if (traditional)
  2820.       obp--;
  2821.     else
  2822.       obp[-1] = ' ';
  2823.       }
  2824.       else
  2825.     *obp++ = '*';
  2826.  
  2827.       {
  2828.     U_CHAR *before_bp = ibp;
  2829.  
  2830.     while (ibp < limit) {
  2831.       switch (*ibp++) {
  2832.       case '/':
  2833.         if (warn_comments && ibp < limit && *ibp == '*')
  2834.           warning ("`/*' within comment");
  2835.         break;
  2836.       case '*':
  2837.         if (*ibp == '\\' && ibp[1] == '\n')
  2838.           newline_fix (ibp);
  2839.         if (ibp >= limit || *ibp == '/')
  2840.           goto comment_end;
  2841.         break;
  2842.       case '\n':
  2843.         ++ip->lineno;
  2844.         /* Copy the newline into the output buffer, in order to
  2845.            avoid the pain of a #line every time a multiline comment
  2846.            is seen.  */
  2847.         if (!put_out_comments)
  2848.           *obp++ = '\n';
  2849.         ++op->lineno;
  2850.       }
  2851.     }
  2852.       comment_end:
  2853.  
  2854.     if (ibp >= limit)
  2855.       error_with_line (line_for_error (start_line),
  2856.                "unterminated comment");
  2857.     else {
  2858.       ibp++;
  2859.       if (put_out_comments) {
  2860.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  2861.         obp += ibp - before_bp;
  2862.       }
  2863.     }
  2864.       }
  2865.       break;
  2866.  
  2867.     case '$':
  2868.       if (!dollars_in_ident)
  2869.     goto randomchar;
  2870.       goto letter;
  2871.  
  2872.     case '0': case '1': case '2': case '3': case '4':
  2873.     case '5': case '6': case '7': case '8': case '9':
  2874.       /* If digit is not part of identifier, it starts a number,
  2875.      which means that following letters are not an identifier.
  2876.      "0x5" does not refer to an identifier "x5".
  2877.      So copy all alphanumerics that follow without accumulating
  2878.      as an identifier.  Periods also, for sake of "3.e7".  */
  2879.  
  2880.       if (ident_length == 0) {
  2881.     while (ibp < limit) {
  2882.       while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  2883.         ++ip->lineno;
  2884.         ibp += 2;
  2885.       }
  2886.       c = *ibp++;
  2887.       /* ".." terminates a preprocessing number.  This is useless for C
  2888.          code but useful for preprocessing other things.  */
  2889.       if (!isalnum (c) && (c != '.' || *ibp == '.') && c != '_') {
  2890.         --ibp;
  2891.         break;
  2892.       }
  2893.       *obp++ = c;
  2894.       /* A sign can be part of a preprocessing number
  2895.          if it follows an e.  */
  2896.       if (c == 'e' || c == 'E') {
  2897.         while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  2898.           ++ip->lineno;
  2899.           ibp += 2;
  2900.         }
  2901.         if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
  2902.           *obp++ = *ibp++;
  2903.           /* But traditional C does not let the token go past the sign.  */
  2904.           if (traditional)
  2905.         break;
  2906.         }
  2907.       }
  2908.     }
  2909.     break;
  2910.       }
  2911.       /* fall through */
  2912.  
  2913.     case '_':
  2914.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  2915.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  2916.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  2917.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  2918.     case 'y': case 'z':
  2919.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  2920.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  2921.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  2922.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  2923.     case 'Y': case 'Z':
  2924.     letter:
  2925.       ident_length++;
  2926.       /* Compute step of hash function, to avoid a proc call on every token */
  2927.       hash = HASHSTEP (hash, c);
  2928.       break;
  2929.  
  2930.     case '\n':
  2931.       if (ip->fname == 0 && *ibp == '-') {
  2932.     /* Newline - inhibits expansion of preceding token.
  2933.        If expanding a macro arg, we keep the newline -.
  2934.        In final output, it is deleted.
  2935.        We recognize Newline - in macro bodies and macro args.  */
  2936.     if (! concatenated) {
  2937.       ident_length = 0;
  2938.       hash = 0;
  2939.     }
  2940.     ibp++;
  2941.     if (!output_marks) {
  2942.       obp--;
  2943.     } else {
  2944.       /* If expanding a macro arg, keep the newline -.  */
  2945.       *obp++ = '-';
  2946.     }
  2947.     break;
  2948.       }
  2949.  
  2950.       /* If reprocessing a macro expansion, newline is a special marker.  */
  2951.       else if (ip->macro != 0) {
  2952.     /* Newline White is a "funny space" to separate tokens that are
  2953.        supposed to be separate but without space between.
  2954.        Here White means any whitespace character.
  2955.        Newline - marks a recursive macro use that is not
  2956.        supposed to be expandable.  */
  2957.  
  2958.     if (is_space[*ibp]) {
  2959.       /* Newline Space does not prevent expansion of preceding token
  2960.          so expand the preceding token and then come back.  */
  2961.       if (ident_length > 0)
  2962.         goto specialchar;
  2963.  
  2964.       /* If generating final output, newline space makes a space.  */
  2965.       if (!output_marks) {
  2966.         obp[-1] = *ibp++;
  2967.         /* And Newline Newline makes a newline, so count it.  */
  2968.         if (obp[-1] == '\n')
  2969.           op->lineno++;
  2970.       } else {
  2971.         /* If expanding a macro arg, keep the newline space.
  2972.            If the arg gets stringified, newline space makes nothing.  */
  2973.         *obp++ = *ibp++;
  2974.       }
  2975.     } else abort ();    /* Newline followed by something random?  */
  2976.     break;
  2977.       }
  2978.  
  2979.       /* If there is a pending identifier, handle it and come back here.  */
  2980.       if (ident_length > 0)
  2981.     goto specialchar;
  2982.  
  2983.       beg_of_line = ibp;
  2984.  
  2985.       /* Update the line counts and output a #line if necessary.  */
  2986.       ++ip->lineno;
  2987.       ++op->lineno;
  2988.       if (ip->lineno != op->lineno) {
  2989.     op->bufp = obp;
  2990.     output_line_command (ip, op, 1, same_file);
  2991.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  2992.     obp = op->bufp;
  2993.       }
  2994.       break;
  2995.  
  2996.       /* Come here either after (1) a null character that is part of the input
  2997.      or (2) at the end of the input, because there is a null there.  */
  2998.     case 0:
  2999.       if (ibp <= limit)
  3000.     /* Our input really contains a null character.  */
  3001.     goto randomchar;
  3002.  
  3003.       /* At end of a macro-expansion level, pop it and read next level.  */
  3004.       if (ip->macro != 0) {
  3005.     obp--;
  3006.     ibp--;
  3007.     /* If traditional, and we have an identifier that ends here,
  3008.        process it now, so we get the right error for recursion.  */
  3009.     if (traditional && ident_length
  3010.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  3011.       redo_char = 1;
  3012.       goto randomchar;
  3013.     }
  3014.     POPMACRO;
  3015.     RECACHE;
  3016.     break;
  3017.       }
  3018.  
  3019.       /* If we don't have a pending identifier,
  3020.      return at end of input.  */
  3021.       if (ident_length == 0) {
  3022.     obp--;
  3023.     ibp--;
  3024.     op->bufp = obp;
  3025.     ip->bufp = ibp;
  3026.     goto ending;
  3027.       }
  3028.  
  3029.       /* If we do have a pending identifier, just consider this null
  3030.      a special character and arrange to dispatch on it again.
  3031.      The second time, IDENT_LENGTH will be zero so we will return.  */
  3032.  
  3033.       /* Fall through */
  3034.  
  3035. specialchar:
  3036.  
  3037.       /* Handle the case of a character such as /, ', " or null
  3038.      seen following an identifier.  Back over it so that
  3039.      after the identifier is processed the special char
  3040.      will be dispatched on again.  */
  3041.  
  3042.       ibp--;
  3043.       obp--;
  3044.       redo_char = 1;
  3045.  
  3046.     default:
  3047.  
  3048. randomchar:
  3049.  
  3050.       if (ident_length > 0) {
  3051.     register HASHNODE *hp;
  3052.  
  3053.     /* We have just seen an identifier end.  If it's a macro, expand it.
  3054.  
  3055.        IDENT_LENGTH is the length of the identifier
  3056.        and HASH is its hash code.
  3057.  
  3058.        The identifier has already been copied to the output,
  3059.        so if it is a macro we must remove it.
  3060.  
  3061.        If REDO_CHAR is 0, the char that terminated the identifier
  3062.        has been skipped in the output and the input.
  3063.        OBP-IDENT_LENGTH-1 points to the identifier.
  3064.        If the identifier is a macro, we must back over the terminator.
  3065.  
  3066.        If REDO_CHAR is 1, the terminating char has already been
  3067.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  3068.  
  3069.     if (!pcp_outfile || pcp_inside_if) {
  3070. startagain:
  3071.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  3072.            hp = hp->next) {
  3073.         
  3074.         if (hp->length == ident_length) {
  3075.           int obufp_before_macroname;
  3076.           int op_lineno_before_macroname;
  3077.           register int i = ident_length;
  3078.           register U_CHAR *p = hp->name;
  3079.           register U_CHAR *q = obp - i;
  3080.           int disabled;
  3081.           
  3082.           if (! redo_char)
  3083.         q--;
  3084.           
  3085.           do {        /* All this to avoid a strncmp () */
  3086.         if (*p++ != *q++)
  3087.           goto hashcollision;
  3088.           } while (--i);
  3089.           
  3090.           /* We found a use of a macro name.
  3091.          see if the context shows it is a macro call.  */
  3092.           
  3093.           /* Back up over terminating character if not already done.  */
  3094.           if (! redo_char) {
  3095.         ibp--;
  3096.         obp--;
  3097.           }
  3098.           
  3099.           /* Save this as a displacement from the beginning of the output
  3100.          buffer.  We can not save this as a position in the output
  3101.          buffer, because it may get realloc'ed by RECACHE.  */
  3102.           obufp_before_macroname = (obp - op->buf) - ident_length;
  3103.           op_lineno_before_macroname = op->lineno;
  3104.           
  3105.           if (hp->type == T_PCSTRING) {
  3106.         pcstring_used (hp); /* Mark the definition of this key
  3107.                        as needed, ensuring that it
  3108.                        will be output.  */
  3109.         break;        /* Exit loop, since the key cannot have a
  3110.                    definition any longer.  */
  3111.           }
  3112.  
  3113.           /* Record whether the macro is disabled.  */
  3114.           disabled = hp->type == T_DISABLED;
  3115.           
  3116.           /* This looks like a macro ref, but if the macro was disabled,
  3117.          just copy its name and put in a marker if requested.  */
  3118.           
  3119.           if (disabled) {
  3120. #if 0
  3121.         /* This error check caught useful cases such as
  3122.            #define foo(x,y) bar (x (y,0), y)
  3123.            foo (foo, baz)  */
  3124.         if (traditional)
  3125.           error ("recursive use of macro `%s'", hp->name);
  3126. #endif
  3127.         
  3128.         if (output_marks) {
  3129.           check_expand (op, limit - ibp + 2);
  3130.           *obp++ = '\n';
  3131.           *obp++ = '-';
  3132.         }
  3133.         break;
  3134.           }
  3135.           
  3136.           /* If macro wants an arglist, verify that a '(' follows.
  3137.          first skip all whitespace, copying it to the output
  3138.          after the macro name.  Then, if there is no '(',
  3139.          decide this is not a macro call and leave things that way.  */
  3140.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  3141.           && hp->value.defn->nargs >= 0)
  3142.         {
  3143.           U_CHAR *old_ibp = ibp;
  3144.           U_CHAR *old_obp = obp;
  3145.           int old_iln = ip->lineno;
  3146.           int old_oln = op->lineno;
  3147.           
  3148.           while (1) {
  3149.             /* Scan forward over whitespace, copying it to the output.  */
  3150.             if (ibp == limit && ip->macro != 0) {
  3151.               POPMACRO;
  3152.               RECACHE;
  3153.               old_ibp = ibp;
  3154.               old_obp = obp;
  3155.               old_iln = ip->lineno;
  3156.               old_oln = op->lineno;
  3157.             }
  3158.             /* A comment: copy it unchanged or discard it.  */
  3159.             else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  3160.               if (put_out_comments) {
  3161.             *obp++ = '/';
  3162.             *obp++ = '*';
  3163.               } else if (! traditional) {
  3164.             *obp++ = ' ';
  3165.               }
  3166.               ibp += 2;
  3167.               while (ibp + 1 != limit
  3168.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  3169.             /* We need not worry about newline-marks,
  3170.                since they are never found in comments.  */
  3171.             if (*ibp == '\n') {
  3172.               /* Newline in a file.  Count it.  */
  3173.               ++ip->lineno;
  3174.               ++op->lineno;
  3175.             }
  3176.             if (put_out_comments)
  3177.               *obp++ = *ibp++;
  3178.             else
  3179.               ibp++;
  3180.               }
  3181.               ibp += 2;
  3182.               if (put_out_comments) {
  3183.             *obp++ = '*';
  3184.             *obp++ = '/';
  3185.               }
  3186.             }
  3187.             else if (is_space[*ibp]) {
  3188.               *obp++ = *ibp++;
  3189.               if (ibp[-1] == '\n') {
  3190.             if (ip->macro == 0) {
  3191.               /* Newline in a file.  Count it.  */
  3192.               ++ip->lineno;
  3193.               ++op->lineno;
  3194.             } else if (!output_marks) {
  3195.               /* A newline mark, and we don't want marks
  3196.                  in the output.  If it is newline-hyphen,
  3197.                  discard it entirely.  Otherwise, it is
  3198.                  newline-whitechar, so keep the whitechar.  */
  3199.               obp--;
  3200.               if (*ibp == '-')
  3201.                 ibp++;
  3202.               else {
  3203.                 if (*ibp == '\n')
  3204.                   ++op->lineno;
  3205.                 *obp++ = *ibp++;
  3206.               }
  3207.             } else {
  3208.               /* A newline mark; copy both chars to the output.  */
  3209.               *obp++ = *ibp++;
  3210.             }
  3211.               }
  3212.             }
  3213.             else break;
  3214.           }
  3215.           if (*ibp != '(') {
  3216.             /* It isn't a macro call.
  3217.                Put back the space that we just skipped.  */
  3218.             ibp = old_ibp;
  3219.             obp = old_obp;
  3220.             ip->lineno = old_iln;
  3221.             op->lineno = old_oln;
  3222.             /* Exit the for loop.  */
  3223.             break;
  3224.           }
  3225.         }
  3226.           
  3227.           /* This is now known to be a macro call.
  3228.          Discard the macro name from the output,
  3229.          along with any following whitespace just copied.  */
  3230.           obp = op->buf + obufp_before_macroname;
  3231.           op->lineno = op_lineno_before_macroname;
  3232.  
  3233.           /* Prevent accidental token-pasting with a character
  3234.          before the macro call.  */
  3235.           if (!traditional && obp != op->buf
  3236.           && (obp[-1] == '-' || obp[1] == '+' || obp[1] == '&'
  3237.               || obp[-1] == '|' || obp[1] == '<' || obp[1] == '>')) {
  3238.         /* If we are expanding a macro arg, make a newline marker
  3239.            to separate the tokens.  If we are making real output,
  3240.            a plain space will do.  */
  3241.         if (output_marks)
  3242.           *obp++ = '\n';
  3243.         *obp++ = ' ';
  3244.           }
  3245.  
  3246.           /* Expand the macro, reading arguments as needed,
  3247.          and push the expansion on the input stack.  */
  3248.           ip->bufp = ibp;
  3249.           op->bufp = obp;
  3250.           macroexpand (hp, op);
  3251.           
  3252.           /* Reexamine input stack, since macroexpand has pushed
  3253.          a new level on it.  */
  3254.           obp = op->bufp;
  3255.           RECACHE;
  3256.           break;
  3257.         }
  3258. hashcollision:
  3259.         ;
  3260.       }            /* End hash-table-search loop */
  3261.     }
  3262.     ident_length = hash = 0; /* Stop collecting identifier */
  3263.     redo_char = 0;
  3264.     concatenated = 0;
  3265.       }                /* End if (ident_length > 0) */
  3266.     }                /* End switch */
  3267.   }                /* End per-char loop */
  3268.  
  3269.   /* Come here to return -- but first give an error message
  3270.      if there was an unterminated successful conditional.  */
  3271.  ending:
  3272.   if (if_stack != ip->if_stack)
  3273.     {
  3274.       char *str = "unknown";
  3275.  
  3276.       switch (if_stack->type)
  3277.     {
  3278.     case T_IF:
  3279.       str = "if";
  3280.       break;
  3281.     case T_IFDEF:
  3282.       str = "ifdef";
  3283.       break;
  3284.     case T_IFNDEF:
  3285.       str = "ifndef";
  3286.       break;
  3287.     case T_ELSE:
  3288.       str = "else";
  3289.       break;
  3290.     case T_ELIF:
  3291.       str = "elif";
  3292.       break;
  3293.     }
  3294.  
  3295.       error_with_line (line_for_error (if_stack->lineno),
  3296.                "unterminated `#%s' conditional", str);
  3297.   }
  3298.   if_stack = ip->if_stack;
  3299. }
  3300.  
  3301. /*
  3302.  * Rescan a string into a temporary buffer and return the result
  3303.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  3304.  *
  3305.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  3306.  * and insert such markers when appropriate.  See `rescan' for details.
  3307.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  3308.  * before substitution; it is 0 for other uses.
  3309.  */
  3310. static FILE_BUF
  3311. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  3312.      U_CHAR *buf, *limit;
  3313.      int output_marks, assertions;
  3314. {
  3315.   register FILE_BUF *ip;
  3316.   FILE_BUF obuf;
  3317.   int length = limit - buf;
  3318.   U_CHAR *buf1;
  3319.   int odepth = indepth;
  3320.   int save_assertions_flag = assertions_flag;
  3321.  
  3322.   assertions_flag = assertions;
  3323.  
  3324.   if (length < 0)
  3325.     abort ();
  3326.  
  3327.   /* Set up the input on the input stack.  */
  3328.  
  3329.   buf1 = (U_CHAR *) alloca (length + 1);
  3330.   {
  3331.     register U_CHAR *p1 = buf;
  3332.     register U_CHAR *p2 = buf1;
  3333.  
  3334.     while (p1 != limit)
  3335.       *p2++ = *p1++;
  3336.   }
  3337.   buf1[length] = 0;
  3338.  
  3339.   /* Set up to receive the output.  */
  3340.  
  3341.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  3342.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  3343.   obuf.fname = 0;
  3344.   obuf.macro = 0;
  3345.   obuf.free_ptr = 0;
  3346.  
  3347.   CHECK_DEPTH ({return obuf;});
  3348.  
  3349.   ++indepth;
  3350.  
  3351.   ip = &instack[indepth];
  3352.   ip->fname = 0;
  3353.   ip->nominal_fname = 0;
  3354.   ip->system_header_p = 0;
  3355.   ip->macro = 0;
  3356.   ip->free_ptr = 0;
  3357.   ip->length = length;
  3358.   ip->buf = ip->bufp = buf1;
  3359.   ip->if_stack = if_stack;
  3360.  
  3361.   ip->lineno = obuf.lineno = 1;
  3362.  
  3363.   /* Scan the input, create the output.  */
  3364.   rescan (&obuf, output_marks);
  3365.  
  3366.   /* Pop input stack to original state.  */
  3367.   --indepth;
  3368.  
  3369.   if (indepth != odepth)
  3370.     abort ();
  3371.  
  3372.   /* Record the output.  */
  3373.   obuf.length = obuf.bufp - obuf.buf;
  3374.  
  3375.   assertions_flag = save_assertions_flag;
  3376.   return obuf;
  3377. }
  3378.  
  3379. /*
  3380.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  3381.  * `#define foo bar'.  Passes to the command handler
  3382.  * (do_define, do_include, etc.): the addresses of the 1st and
  3383.  * last chars of the command (starting immediately after the #
  3384.  * keyword), plus op and the keyword table pointer.  If the command
  3385.  * contains comments it is copied into a temporary buffer sans comments
  3386.  * and the temporary buffer is passed to the command handler instead.
  3387.  * Likewise for backslash-newlines.
  3388.  *
  3389.  * Returns nonzero if this was a known # directive.
  3390.  * Otherwise, returns zero, without advancing the input pointer.
  3391.  */
  3392.  
  3393. static int
  3394. handle_directive (ip, op)
  3395.      FILE_BUF *ip, *op;
  3396. {
  3397.   register U_CHAR *bp, *cp;
  3398.   register struct directive *kt;
  3399.   register int ident_length;
  3400.   U_CHAR *resume_p;
  3401.  
  3402.   /* Nonzero means we must copy the entire command
  3403.      to get rid of comments or backslash-newlines.  */
  3404.   int copy_command = 0;
  3405.  
  3406.   U_CHAR *ident, *after_ident;
  3407.  
  3408.   bp = ip->bufp;
  3409.  
  3410.   /* Record where the directive started.  do_xifdef needs this.  */
  3411.   directive_start = bp - 1;
  3412.  
  3413.   /* Skip whitespace and \-newline.  */
  3414.   while (1) {
  3415.     if (is_hor_space[*bp]) {
  3416.       if ((*bp == '\f' || *bp == '\v') && pedantic)
  3417.     pedwarn ("%s in preprocessing directive",
  3418.          *bp == '\f' ? "formfeed" : "vertical tab");
  3419.       bp++;
  3420.     } else if (*bp == '/' && (bp[1] == '*'
  3421.                   || (cplusplus_comments && bp[1] == '/'))) {
  3422.       ip->bufp = bp + 2;
  3423.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  3424.       bp = ip->bufp;
  3425.     } else if (*bp == '\\' && bp[1] == '\n') {
  3426.       bp += 2; ip->lineno++;
  3427.     } else break;
  3428.   }
  3429.  
  3430.   /* Now find end of directive name.
  3431.      If we encounter a backslash-newline, exchange it with any following
  3432.      symbol-constituents so that we end up with a contiguous name.  */
  3433.  
  3434.   cp = bp;
  3435.   while (1) {
  3436.     if (is_idchar[*cp])
  3437.       cp++;
  3438.     else {
  3439.       if (*cp == '\\' && cp[1] == '\n')
  3440.     name_newline_fix (cp);
  3441.       if (is_idchar[*cp])
  3442.     cp++;
  3443.       else break;
  3444.     }
  3445.   }
  3446.   ident_length = cp - bp;
  3447.   ident = bp;
  3448.   after_ident = cp;
  3449.  
  3450.   /* A line of just `#' becomes blank.  */
  3451.  
  3452.   if (ident_length == 0 && *after_ident == '\n') {
  3453.     ip->bufp = after_ident;
  3454.     return 1;
  3455.   }
  3456.  
  3457.   if (ident_length == 0 || !is_idstart[*ident]) {
  3458.     U_CHAR *p = ident;
  3459.     while (is_idchar[*p]) {
  3460.       if (*p < '0' || *p > '9')
  3461.     break;
  3462.       p++;
  3463.     }
  3464.     /* Handle # followed by a line number.  */
  3465.     if (p != ident && !is_idchar[*p]) {
  3466.       static struct directive line_directive_table[] = {
  3467.     {  4, do_line, "line", T_LINE},
  3468.       };
  3469.       if (pedantic)
  3470.     pedwarn ("`#' followed by integer");
  3471.       after_ident = ident;
  3472.       kt = line_directive_table;
  3473.       goto old_linenum;
  3474.     }
  3475.  
  3476.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  3477.     if (p == ident) {
  3478.       while (*p == '#' || is_hor_space[*p]) p++;
  3479.       if (*p == '\n') {
  3480.     if (pedantic && !lang_asm)
  3481.       warning ("invalid preprocessor directive");
  3482.     return 0;
  3483.       }
  3484.     }
  3485.  
  3486.     if (!lang_asm)
  3487.       error ("invalid preprocessor directive name");
  3488.  
  3489.     return 0;
  3490.   }
  3491.  
  3492.   /*
  3493.    * Decode the keyword and call the appropriate expansion
  3494.    * routine, after moving the input pointer up to the next line.
  3495.    */
  3496.   for (kt = directive_table; kt->length > 0; kt++) {
  3497.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  3498.       register U_CHAR *buf;
  3499.       register U_CHAR *limit;
  3500.       int unterminated;
  3501.       int junk;
  3502.       int *already_output;
  3503.  
  3504.       /* Nonzero means do not delete comments within the directive.
  3505.      #define needs this when -traditional.  */
  3506.       int keep_comments;
  3507.  
  3508.     old_linenum:
  3509.  
  3510.       limit = ip->buf + ip->length;
  3511.       unterminated = 0;
  3512.       already_output = 0;
  3513.       keep_comments = traditional && kt->traditional_comments;
  3514.       /* #import is defined only in Objective C, or when on the NeXT.  */
  3515.       if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
  3516.     break;
  3517.  
  3518.       /* Find the end of this command (first newline not backslashed
  3519.      and not in a string or comment).
  3520.      Set COPY_COMMAND if the command must be copied
  3521.      (it contains a backslash-newline or a comment).  */
  3522.  
  3523.       buf = bp = after_ident;
  3524.       while (bp < limit) {
  3525.     register U_CHAR c = *bp++;
  3526.     switch (c) {
  3527.     case '\\':
  3528.       if (bp < limit) {
  3529.         if (*bp == '\n') {
  3530.           ip->lineno++;
  3531.           copy_command = 1;
  3532.         }
  3533.         bp++;
  3534.       }
  3535.       break;
  3536.  
  3537.     case '\'':
  3538.     case '\"':
  3539.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  3540.       /* Don't bother calling the directive if we already got an error
  3541.          message due to unterminated string.  Skip everything and pretend
  3542.          we called the directive.  */
  3543.       if (unterminated) {
  3544.         if (traditional) {
  3545.           /* Traditional preprocessing permits unterminated strings.  */
  3546.           ip->bufp = bp;
  3547.           goto endloop1;
  3548.         }
  3549.         ip->bufp = bp;
  3550.         return 1;
  3551.       }
  3552.       break;
  3553.  
  3554.       /* <...> is special for #include.  */
  3555.     case '<':
  3556.       if (!kt->angle_brackets)
  3557.         break;
  3558.       while (*bp && *bp != '>') bp++;
  3559.       break;
  3560.  
  3561.     case '/':
  3562.       if (*bp == '\\' && bp[1] == '\n')
  3563.         newline_fix (bp);
  3564.       if (*bp == '*'
  3565.           || (cplusplus_comments && *bp == '/')) {
  3566.         U_CHAR *obp = bp - 1;
  3567.         ip->bufp = bp + 1;
  3568.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  3569.         bp = ip->bufp;
  3570.         /* No need to copy the command because of a comment at the end;
  3571.            just don't include the comment in the directive.  */
  3572.         if (bp == limit || *bp == '\n') {
  3573.           bp = obp;
  3574.           goto endloop1;
  3575.         }
  3576.         /* Don't remove the comments if -traditional.  */
  3577.         if (! keep_comments)
  3578.           copy_command++;
  3579.       }
  3580.       break;
  3581.  
  3582.     case '\f':
  3583.     case '\v':
  3584.       if (pedantic)
  3585.         pedwarn ("%s in preprocessing directive",
  3586.              c == '\f' ? "formfeed" : "vertical tab");
  3587.       break;
  3588.  
  3589.     case '\n':
  3590.       --bp;        /* Point to the newline */
  3591.       ip->bufp = bp;
  3592.       goto endloop1;
  3593.     }
  3594.       }
  3595.       ip->bufp = bp;
  3596.  
  3597.     endloop1:
  3598.       resume_p = ip->bufp;
  3599.       /* BP is the end of the directive.
  3600.      RESUME_P is the next interesting data after the directive.
  3601.      A comment may come between.  */
  3602.  
  3603.       /* If a directive should be copied through, and -E was given,
  3604.      pass it through before removing comments.  */
  3605.       if (!no_output && kt->pass_thru && put_out_comments) {
  3606.         int len;
  3607.  
  3608.     /* Output directive name.  */
  3609.         check_expand (op, kt->length + 2);
  3610.     /* Make sure # is at the start of a line */
  3611.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  3612.       op->lineno++;
  3613.       *op->bufp++ = '\n';
  3614.     }
  3615.         *op->bufp++ = '#';
  3616.         bcopy (kt->name, op->bufp, kt->length);
  3617.         op->bufp += kt->length;
  3618.  
  3619.     /* Output arguments.  */
  3620.     len = (bp - buf);
  3621.     check_expand (op, len);
  3622.     bcopy (buf, (char *) op->bufp, len);
  3623.     op->bufp += len;
  3624.     /* Take account of any (escaped) newlines just output.  */
  3625.     while (--len >= 0)
  3626.       if (buf[len] == '\n')
  3627.         op->lineno++;
  3628.  
  3629.     already_output = &junk;
  3630.       }                /* Don't we need a newline or #line? */
  3631.  
  3632.       if (copy_command) {
  3633.     register U_CHAR *xp = buf;
  3634.     /* Need to copy entire command into temp buffer before dispatching */
  3635.  
  3636.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  3637.                           some slop */
  3638.     buf = cp;
  3639.  
  3640.     /* Copy to the new buffer, deleting comments
  3641.        and backslash-newlines (and whitespace surrounding the latter).  */
  3642.  
  3643.     while (xp < bp) {
  3644.       register U_CHAR c = *xp++;
  3645.       *cp++ = c;
  3646.  
  3647.       switch (c) {
  3648.       case '\n':
  3649.         abort ();  /* A bare newline should never part of the line.  */
  3650.         break;
  3651.  
  3652.         /* <...> is special for #include.  */
  3653.       case '<':
  3654.         if (!kt->angle_brackets)
  3655.           break;
  3656.         while (xp < bp && c != '>') {
  3657.           c = *xp++;
  3658.           if (c == '\\' && xp < bp && *xp == '\n')
  3659.         xp++;
  3660.           else
  3661.         *cp++ = c;
  3662.         }
  3663.         break;
  3664.  
  3665.       case '\\':
  3666.         if (*xp == '\n') {
  3667.           xp++;
  3668.           cp--;
  3669.           if (cp != buf && is_space[cp[-1]]) {
  3670.         while (cp != buf && is_space[cp[-1]]) cp--;
  3671.         cp++;
  3672.         SKIP_WHITE_SPACE (xp);
  3673.           } else if (is_space[*xp]) {
  3674.         *cp++ = *xp++;
  3675.         SKIP_WHITE_SPACE (xp);
  3676.           }
  3677.         } else {
  3678.           *cp++ = *xp++;
  3679.         }
  3680.         break;
  3681.  
  3682.       case '\'':
  3683.       case '\"':
  3684.         {
  3685.           register U_CHAR *bp1
  3686.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  3687.                       NULL_PTR, NULL_PTR, NULL_PTR);
  3688.           while (xp != bp1)
  3689.         if (*xp == '\\') {
  3690.           if (*++xp != '\n')
  3691.             *cp++ = '\\';
  3692.           else
  3693.             xp++;
  3694.         } else
  3695.           *cp++ = *xp++;
  3696.         }
  3697.         break;
  3698.  
  3699.       case '/':
  3700.         if (*xp == '*'
  3701.         || (cplusplus_comments && *xp == '/')) {
  3702.           ip->bufp = xp + 1;
  3703.           /* If we already copied the command through,
  3704.          already_output != 0 prevents outputting comment now.  */
  3705.           skip_to_end_of_comment (ip, already_output, 0);
  3706.           if (keep_comments)
  3707.         while (xp != ip->bufp)
  3708.           *cp++ = *xp++;
  3709.           /* Delete or replace the slash.  */
  3710.           else if (traditional)
  3711.         cp--;
  3712.           else
  3713.         cp[-1] = ' ';
  3714.           xp = ip->bufp;
  3715.         }
  3716.       }
  3717.     }
  3718.  
  3719.     /* Null-terminate the copy.  */
  3720.  
  3721.     *cp = 0;
  3722.       } else
  3723.     cp = bp;
  3724.  
  3725.       ip->bufp = resume_p;
  3726.  
  3727.       /* Some directives should be written out for cc1 to process,
  3728.      just as if they were not defined.  And sometimes we're copying
  3729.      definitions through.  */
  3730.  
  3731.       if (!no_output && already_output == 0
  3732.       && (kt->pass_thru
  3733.           || (kt->type == T_DEFINE
  3734.           && (dump_macros == dump_names
  3735.               || dump_macros == dump_definitions)))) {
  3736.         int len;
  3737.  
  3738.     /* Output directive name.  */
  3739.         check_expand (op, kt->length + 1);
  3740.         *op->bufp++ = '#';
  3741.         bcopy (kt->name, (char *) op->bufp, kt->length);
  3742.         op->bufp += kt->length;
  3743.  
  3744.     if (kt->pass_thru || dump_macros == dump_definitions) {
  3745.       /* Output arguments.  */
  3746.       len = (cp - buf);
  3747.       check_expand (op, len);
  3748.       bcopy (buf, (char *) op->bufp, len);
  3749.       op->bufp += len;
  3750.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  3751.       U_CHAR *xp = buf;
  3752.       U_CHAR *yp;
  3753.       SKIP_WHITE_SPACE (xp);
  3754.       yp = xp;
  3755.       while (is_idchar[*xp]) xp++;
  3756.       len = (xp - yp);
  3757.       check_expand (op, len + 1);
  3758.       *op->bufp++ = ' ';
  3759.       bcopy (yp, op->bufp, len);
  3760.       op->bufp += len;
  3761.     }
  3762.       }                /* Don't we need a newline or #line? */
  3763.  
  3764.       /* Call the appropriate command handler.  buf now points to
  3765.      either the appropriate place in the input buffer, or to
  3766.      the temp buffer if it was necessary to make one.  cp
  3767.      points to the first char after the contents of the (possibly
  3768.      copied) command, in either case. */
  3769.       (*kt->func) (buf, cp, op, kt);
  3770.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  3771.  
  3772.       return 1;
  3773.     }
  3774.   }
  3775.  
  3776.   /* It is deliberate that we don't warn about undefined directives.
  3777.      That is the responsibility of cc1.  */
  3778.   return 0;
  3779. }
  3780.  
  3781. static struct tm *
  3782. timestamp ()
  3783. {
  3784.   static struct tm *timebuf;
  3785.   if (!timebuf) {
  3786.     time_t t = time (0);
  3787.     timebuf = localtime (&t);
  3788.   }
  3789.   return timebuf;
  3790. }
  3791.  
  3792. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  3793.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  3794.                 };
  3795.  
  3796. /*
  3797.  * expand things like __FILE__.  Place the expansion into the output
  3798.  * buffer *without* rescanning.
  3799.  */
  3800.  
  3801. static void
  3802. special_symbol (hp, op)
  3803.      HASHNODE *hp;
  3804.      FILE_BUF *op;
  3805. {
  3806.   char *buf;
  3807.   int i, len;
  3808.   int true_indepth;
  3809.   FILE_BUF *ip = NULL;
  3810.   struct tm *timebuf;
  3811.  
  3812.   int paren = 0;        /* For special `defined' keyword */
  3813.  
  3814.   if (pcp_outfile && pcp_inside_if
  3815.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  3816.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  3817.        hp->name);
  3818.     
  3819.   for (i = indepth; i >= 0; i--)
  3820.     if (instack[i].fname != NULL) {
  3821.       ip = &instack[i];
  3822.       break;
  3823.     }
  3824.   if (ip == NULL) {
  3825.     error ("cccp error: not in any file?!");
  3826.     return;            /* the show must go on */
  3827.   }
  3828.  
  3829.   switch (hp->type) {
  3830.   case T_FILE:
  3831.   case T_BASE_FILE:
  3832.     {
  3833.       char *string;
  3834.       if (hp->type == T_FILE)
  3835.     string = ip->nominal_fname;
  3836.       else
  3837.     string = instack[0].nominal_fname;
  3838.  
  3839.       if (string)
  3840.     {
  3841.       buf = (char *) alloca (3 + 4 * strlen (string));
  3842.       quote_string (buf, string);
  3843.     }
  3844.       else
  3845.     buf = "\"\"";
  3846.  
  3847.       break;
  3848.     }
  3849.  
  3850.   case T_INCLUDE_LEVEL:
  3851.     true_indepth = 0;
  3852.     for (i = indepth; i >= 0; i--)
  3853.       if (instack[i].fname != NULL)
  3854.         true_indepth++;
  3855.  
  3856.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  3857.     sprintf (buf, "%d", true_indepth - 1);
  3858.     break;
  3859.  
  3860.   case T_VERSION:
  3861.     buf = (char *) alloca (3 + strlen (version_string));
  3862.     sprintf (buf, "\"%s\"", version_string);
  3863.     break;
  3864.  
  3865. #ifndef NO_BUILTIN_SIZE_TYPE
  3866.   case T_SIZE_TYPE:
  3867.     buf = SIZE_TYPE;
  3868.     break;
  3869. #endif
  3870.  
  3871. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  3872.   case T_PTRDIFF_TYPE:
  3873.     buf = PTRDIFF_TYPE;
  3874.     break;
  3875. #endif
  3876.  
  3877.   case T_WCHAR_TYPE:
  3878.     buf = wchar_type;
  3879.     break;
  3880.  
  3881.   case T_USER_LABEL_PREFIX_TYPE:
  3882.     buf = USER_LABEL_PREFIX;
  3883.     break;
  3884.  
  3885.   case T_REGISTER_PREFIX_TYPE:
  3886.     buf = REGISTER_PREFIX;
  3887.     break;
  3888.  
  3889.   case T_CONST:
  3890.     buf = (char *) alloca (4 * sizeof (int));
  3891.     sprintf (buf, "%d", hp->value.ival);
  3892.     if (pcp_inside_if && pcp_outfile)
  3893.       /* Output a precondition for this macro use */
  3894.       fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
  3895.     break;
  3896.  
  3897.   case T_SPECLINE:
  3898.     buf = (char *) alloca (10);
  3899.     sprintf (buf, "%d", ip->lineno);
  3900.     break;
  3901.  
  3902.   case T_DATE:
  3903.   case T_TIME:
  3904.     buf = (char *) alloca (20);
  3905.     timebuf = timestamp ();
  3906.     if (hp->type == T_DATE)
  3907.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  3908.           timebuf->tm_mday, timebuf->tm_year + 1900);
  3909.     else
  3910.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  3911.           timebuf->tm_sec);
  3912.     break;
  3913.  
  3914.   case T_SPEC_DEFINED:
  3915.     buf = " 0 ";        /* Assume symbol is not defined */
  3916.     ip = &instack[indepth];
  3917.     SKIP_WHITE_SPACE (ip->bufp);
  3918.     if (*ip->bufp == '(') {
  3919.       paren++;
  3920.       ip->bufp++;            /* Skip over the paren */
  3921.       SKIP_WHITE_SPACE (ip->bufp);
  3922.     }
  3923.  
  3924.     if (!is_idstart[*ip->bufp])
  3925.       goto oops;
  3926.     if (hp = lookup (ip->bufp, -1, -1)) {
  3927.       if (pcp_outfile && pcp_inside_if
  3928.       && hp->value.defn->predefined)
  3929.     /* Output a precondition for this macro use. */
  3930.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  3931.       buf = " 1 ";
  3932.     }
  3933.     else
  3934.       if (pcp_outfile && pcp_inside_if)    {
  3935.     /* Output a precondition for this macro use */
  3936.     U_CHAR *cp = ip->bufp;
  3937.     fprintf (pcp_outfile, "#undef ");
  3938.     while (is_idchar[*cp]) /* Ick! */
  3939.       fputc (*cp++, pcp_outfile);
  3940.     putc ('\n', pcp_outfile);
  3941.       }
  3942.     while (is_idchar[*ip->bufp])
  3943.       ++ip->bufp;
  3944.     SKIP_WHITE_SPACE (ip->bufp);
  3945.     if (paren) {
  3946.       if (*ip->bufp != ')')
  3947.     goto oops;
  3948.       ++ip->bufp;
  3949.     }
  3950.     break;
  3951.  
  3952. oops:
  3953.  
  3954.     error ("`defined' without an identifier");
  3955.     break;
  3956.  
  3957.   default:
  3958.     error ("cccp error: invalid special hash type"); /* time for gdb */
  3959.     abort ();
  3960.   }
  3961.   len = strlen (buf);
  3962.   check_expand (op, len);
  3963.   bcopy (buf, (char *) op->bufp, len);
  3964.   op->bufp += len;
  3965.  
  3966.   return;
  3967. }
  3968.  
  3969.  
  3970. /* Routines to handle #directives */
  3971.  
  3972. /* Handle #include and #import.
  3973.    This function expects to see "fname" or <fname> on the input.  */
  3974.  
  3975. static int
  3976. do_include (buf, limit, op, keyword)
  3977.      U_CHAR *buf, *limit;
  3978.      FILE_BUF *op;
  3979.      struct directive *keyword;
  3980. {
  3981.   int importing = (keyword->type == T_IMPORT);
  3982.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  3983.   static int import_warning = 0;
  3984.   char *fname;        /* Dynamically allocated fname buffer */
  3985.   char *pcftry;
  3986.   char *pcfname;
  3987.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  3988.  
  3989.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  3990.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  3991.   struct file_name_list *searchptr = 0;
  3992.   int flen;
  3993.  
  3994.   int f;            /* file number */
  3995.  
  3996.   int retried = 0;        /* Have already tried macro
  3997.                    expanding the include line*/
  3998.   FILE_BUF trybuf;        /* It got expanded into here */
  3999.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4000.   int pcf = -1;
  4001.   char *pcfbuf;
  4002.   int pcfbuflimit;
  4003.   int pcfnum;
  4004.   f= -1;            /* JF we iz paranoid! */
  4005.  
  4006.   if (importing && warn_import && !inhibit_warnings
  4007.       && !instack[indepth].system_header_p && !import_warning) {
  4008.     import_warning = 1;
  4009.     warning ("using `#import' is not recommended");
  4010.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4011.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4012.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4013.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4014.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4015.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4016.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4017.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4018.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4019.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4020.   }
  4021.  
  4022. get_filename:
  4023.  
  4024.   fbeg = buf;
  4025.   SKIP_WHITE_SPACE (fbeg);
  4026.   /* Discard trailing whitespace so we can easily see
  4027.      if we have parsed all the significant chars we were given.  */
  4028.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4029.  
  4030.   switch (*fbeg++) {
  4031.   case '\"':
  4032.     {
  4033.       FILE_BUF *fp;
  4034.       /* Copy the operand text, concatenating the strings.  */
  4035.       {
  4036.     U_CHAR *fin = fbeg;
  4037.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4038.     fend = fbeg;
  4039.     while (fin != limit) {
  4040.       while (fin != limit && *fin != '\"')
  4041.         *fend++ = *fin++;
  4042.       fin++;
  4043.       if (fin == limit)
  4044.         break;
  4045.       /* If not at the end, there had better be another string.  */
  4046.       /* Skip just horiz space, and don't go past limit.  */
  4047.       while (fin != limit && is_hor_space[*fin]) fin++;
  4048.       if (fin != limit && *fin == '\"')
  4049.         fin++;
  4050.       else
  4051.         goto fail;
  4052.     }
  4053.       }
  4054.       *fend = 0;
  4055.  
  4056.       /* We have "filename".  Figure out directory this source
  4057.      file is coming from and put it on the front of the list. */
  4058.  
  4059.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4060.       if (ignore_srcdir) break;
  4061.  
  4062.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4063.     {
  4064.       int n;
  4065.       char *ep,*nam;
  4066.  
  4067.       if ((nam = fp->nominal_fname) != NULL) {
  4068.         /* Found a named file.  Figure out dir of the file,
  4069.            and put it in front of the search list.  */
  4070.         dsp[0].next = search_start;
  4071.         search_start = dsp;
  4072. #ifndef VMS
  4073.         ep = rindex (nam, '/');
  4074. #else                /* VMS */
  4075.         ep = rindex (nam, ']');
  4076.         if (ep == NULL) ep = rindex (nam, '>');
  4077.         if (ep == NULL) ep = rindex (nam, ':');
  4078.         if (ep != NULL) ep++;
  4079. #endif                /* VMS */
  4080.         if (ep != NULL) {
  4081.           n = ep - nam;
  4082.           dsp[0].fname = (char *) alloca (n + 1);
  4083.           strncpy (dsp[0].fname, nam, n);
  4084.           dsp[0].fname[n] = '\0';
  4085.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4086.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4087.         } else {
  4088.           dsp[0].fname = 0; /* Current directory */
  4089.         }
  4090.         dsp[0].got_name_map = 0;
  4091.         break;
  4092.       }
  4093.     }
  4094.       break;
  4095.     }
  4096.  
  4097.   case '<':
  4098.     fend = fbeg;
  4099.     while (fend != limit && *fend != '>') fend++;
  4100.     if (*fend == '>' && fend + 1 == limit) {
  4101.       angle_brackets = 1;
  4102.       /* If -I-, start with the first -I dir after the -I-.  */
  4103.       if (first_bracket_include)
  4104.     search_start = first_bracket_include;
  4105.       break;
  4106.     }
  4107.     goto fail;
  4108.  
  4109.   default:
  4110. #ifdef VMS
  4111.     /*
  4112.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  4113.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  4114.      * code from case '<' is repeated here) and generates a warning.
  4115.      */
  4116.     if (isalpha(*(--fbeg))) {
  4117.       fend = fbeg;
  4118.       while (fend != limit && (!isspace(*fend))) fend++;
  4119.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  4120.       if (fend  == limit) {
  4121.     angle_brackets = 1;
  4122.     /* If -I-, start with the first -I dir after the -I-.  */
  4123.     if (first_bracket_include)
  4124.       search_start = first_bracket_include;
  4125.     break;
  4126.       }
  4127.     }
  4128. #endif
  4129.  
  4130.   fail:
  4131.     if (retried) {
  4132.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  4133.       return 0;
  4134.     } else {
  4135.       trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  4136.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  4137.       bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  4138.       limit = buf + (trybuf.bufp - trybuf.buf);
  4139.       free (trybuf.buf);
  4140.       retried++;
  4141.       goto get_filename;
  4142.     }
  4143.   }
  4144.  
  4145.   /* For #include_next, skip in the search path
  4146.      past the dir in which the containing file was found.  */
  4147.   if (skip_dirs) {
  4148.     FILE_BUF *fp;
  4149.     for (fp = &instack[indepth]; fp >= instack; fp--)
  4150.       if (fp->fname != NULL) {
  4151.     /* fp->dir is null if the containing file was specified
  4152.        with an absolute file name.  In that case, don't skip anything.  */
  4153.     if (fp->dir)
  4154.       search_start = fp->dir->next;
  4155.     break;
  4156.       }
  4157.   }
  4158.  
  4159.   flen = fend - fbeg;
  4160.  
  4161.   if (flen == 0)
  4162.     {
  4163.       error ("empty file name in `#%s'", keyword->name);
  4164.       return 0;
  4165.     }
  4166.  
  4167.   /* Allocate this permanently, because it gets stored in the definitions
  4168.      of macros.  */
  4169.   fname = (char *) xmalloc (max_include_len + flen + 4);
  4170.   /* + 2 above for slash and terminating null.  */
  4171.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  4172.  
  4173.   /* If specified file name is absolute, just open it.  */
  4174.  
  4175.   if (*fbeg == '/') {
  4176.     strncpy (fname, fbeg, flen);
  4177.     fname[flen] = 0;
  4178.     if (redundant_include_p (fname))
  4179.       return 0;
  4180.     if (importing)
  4181.       f = lookup_import (fname, NULL_PTR);
  4182.     else
  4183.       f = open_include_file (fname, NULL_PTR);
  4184.     if (f == -2)
  4185.       return 0;        /* Already included this file */
  4186.   } else {
  4187.     /* Search directory path, trying to open the file.
  4188.        Copy each filename tried into FNAME.  */
  4189.  
  4190.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  4191.       if (searchptr->fname) {
  4192.     /* The empty string in a search path is ignored.
  4193.        This makes it possible to turn off entirely
  4194.        a standard piece of the list.  */
  4195.     if (searchptr->fname[0] == 0)
  4196.       continue;
  4197.     strcpy (fname, searchptr->fname);
  4198.     strcat (fname, "/");
  4199.     fname[strlen (fname) + flen] = 0;
  4200.       } else {
  4201.     fname[0] = 0;
  4202.       }
  4203.       strncat (fname, fbeg, flen);
  4204. #ifdef VMS
  4205.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  4206.          full VMS file specification */
  4207.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  4208.     /* Fix up the filename */
  4209.     hack_vms_include_specification (fname);
  4210.       } else {
  4211.           /* This is a normal VMS filespec, so use it unchanged.  */
  4212.     strncpy (fname, fbeg, flen);
  4213.     fname[flen] = 0;
  4214.     /* if it's '#include filename', add the missing .h */
  4215.     if (index(fname,'.')==NULL) {
  4216.       strcat (fname, ".h");
  4217.     }
  4218.       }
  4219. #endif /* VMS */
  4220.       if (importing)
  4221.     f = lookup_import (fname, searchptr);
  4222.       else
  4223.     f = open_include_file (fname, searchptr);
  4224.       if (f == -2)
  4225.     return 0;            /* Already included this file */
  4226. #ifdef EACCES
  4227.       else if (f == -1 && errno == EACCES)
  4228.     warning ("Header file %s exists, but is not readable", fname);
  4229. #endif
  4230.       if (redundant_include_p (fname)) {
  4231.     close (f);
  4232.     return 0;
  4233.       }
  4234.       if (f >= 0)
  4235.     break;
  4236.     }
  4237.   }
  4238.  
  4239.   if (f < 0) {
  4240.     /* A file that was not found.  */
  4241.  
  4242.     strncpy (fname, fbeg, flen);
  4243.     fname[flen] = 0;
  4244.     /* If generating dependencies and -MG was specified, we assume missing
  4245.        files are leaf files, living in the same directory as the source file
  4246.        or other similar place; these missing files may be generated from
  4247.        other files and may not exist yet (eg: y.tab.h).  */
  4248.     if (print_deps_missing_files
  4249.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  4250.       {
  4251.     /* If it was requested as a system header file,
  4252.        then assume it belongs in the first place to look for such.  */
  4253.     if (angle_brackets)
  4254.       {
  4255.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  4256.           {
  4257.         if (searchptr->fname)
  4258.           {
  4259.             char *p;
  4260.  
  4261.             if (searchptr->fname[0] == 0)
  4262.               continue;
  4263.             p = xmalloc (strlen (searchptr->fname)
  4264.                  + strlen (fname) + 2);
  4265.             strcpy (p, searchptr->fname);
  4266.             strcat (p, "/");
  4267.             strcat (p, fname);
  4268.             deps_output (p, ' ');
  4269.             break;
  4270.           }
  4271.           }
  4272.       }
  4273.     else
  4274.       {
  4275.         /* Otherwise, omit the directory, as if the file existed
  4276.            in the directory with the source.  */
  4277.         deps_output (fname, ' ');
  4278.       }
  4279.       }
  4280.     /* If -M was specified, and this header file won't be added to the
  4281.        dependency list, then don't count this as an error, because we can
  4282.        still produce correct output.  Otherwise, we can't produce correct
  4283.        output, because there may be dependencies we need inside the missing
  4284.        file, and we don't know what directory this missing file exists in.  */
  4285.     else if (print_deps
  4286.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  4287.       warning ("No include path in which to find %s", fname);
  4288.     else if (search_start)
  4289.       error_from_errno (fname);
  4290.     else
  4291.       error ("No include path in which to find %s", fname);
  4292.   } else {
  4293.     struct stat stat_f;
  4294.  
  4295.     /* Check to see if this include file is a once-only include file.
  4296.        If so, give up.  */
  4297.  
  4298.     struct file_name_list* ptr;
  4299.  
  4300.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  4301.       if (!strcmp (ptr->fname, fname)) {
  4302.     close (f);
  4303.         return 0;                /* This file was once'd. */
  4304.       }
  4305.     }
  4306.  
  4307.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  4308.       if (!strcmp (ptr->fname, fname))
  4309.         break;                /* This file was included before. */
  4310.     }
  4311.  
  4312.     if (ptr == 0) {
  4313.       /* This is the first time for this file.  */
  4314.       /* Add it to list of files included.  */
  4315.  
  4316.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  4317.       ptr->control_macro = 0;
  4318.       ptr->c_system_include_path = 0;
  4319.       ptr->next = all_include_files;
  4320.       all_include_files = ptr;
  4321.       ptr->fname = savestring (fname);
  4322.       ptr->got_name_map = 0;
  4323.  
  4324.       /* For -M, add this file to the dependencies.  */
  4325.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  4326.     deps_output (fname, ' ');
  4327.     }   
  4328.  
  4329.     /* Handle -H option.  */
  4330.     if (print_include_names) {
  4331.       output_dots (stderr, indepth);
  4332.       fprintf (stderr, "%s\n", fname);
  4333.     }
  4334.  
  4335.     if (angle_brackets)
  4336.       system_include_depth++;
  4337.  
  4338.     /* Actually process the file.  */
  4339.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  4340.  
  4341.     pcftry = (char *) alloca (strlen (fname) + 30);
  4342.     pcfbuf = 0;
  4343.     pcfnum = 0;
  4344.  
  4345.     fstat (f, &stat_f);
  4346.  
  4347.     if (!no_precomp)
  4348.       do {
  4349.     sprintf (pcftry, "%s%d", fname, pcfnum++);
  4350.     
  4351.     pcf = open (pcftry, O_RDONLY, 0666);
  4352.     if (pcf != -1)
  4353.       {
  4354.         struct stat s;
  4355.  
  4356.         fstat (pcf, &s);
  4357.         if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  4358.               sizeof (s.st_ino))
  4359.         || stat_f.st_dev != s.st_dev)
  4360.           {
  4361.         pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  4362.         /* Don't need it any more.  */
  4363.         close (pcf);
  4364.           }
  4365.         else
  4366.           {
  4367.         /* Don't need it at all.  */
  4368.         close (pcf);
  4369.         break;
  4370.           }
  4371.       }
  4372.       } while (pcf != -1 && !pcfbuf);
  4373.     
  4374.     /* Actually process the file */
  4375.     if (pcfbuf) {
  4376.       pcfname = xmalloc (strlen (pcftry) + 1);
  4377.       strcpy (pcfname, pcftry);
  4378.       pcfinclude (pcfbuf, pcfbuflimit, fname, op);
  4379.     }
  4380.     else
  4381.       finclude (f, fname, op, is_system_include (fname), searchptr);
  4382.  
  4383.     if (angle_brackets)
  4384.       system_include_depth--;
  4385.   }
  4386.   return 0;
  4387. }
  4388.  
  4389. /* Return nonzero if there is no need to include file NAME
  4390.    because it has already been included and it contains a conditional
  4391.    to make a repeated include do nothing.  */
  4392.  
  4393. static int
  4394. redundant_include_p (name)
  4395.      char *name;
  4396. {
  4397.   struct file_name_list *l = all_include_files;
  4398.   for (; l; l = l->next)
  4399.     if (! strcmp (name, l->fname)
  4400.     && l->control_macro
  4401.     && lookup (l->control_macro, -1, -1))
  4402.       return 1;
  4403.   return 0;
  4404. }
  4405.  
  4406. /* Return nonzero if the given FILENAME is an absolute pathname which
  4407.    designates a file within one of the known "system" include file
  4408.    directories.  We assume here that if the given FILENAME looks like
  4409.    it is the name of a file which resides either directly in a "system"
  4410.    include file directory, or within any subdirectory thereof, then the
  4411.    given file must be a "system" include file.  This function tells us
  4412.    if we should suppress pedantic errors/warnings for the given FILENAME.
  4413.  
  4414.    The value is 2 if the file is a C-language system header file
  4415.    for which C++ should (on most systems) assume `extern "C"'.  */
  4416.  
  4417. static int
  4418. is_system_include (filename)
  4419.     register char *filename;
  4420. {
  4421.   struct file_name_list *searchptr;
  4422.  
  4423.   for (searchptr = first_system_include; searchptr;
  4424.        searchptr = searchptr->next)
  4425.     if (searchptr->fname) {
  4426.       register char *sys_dir = searchptr->fname;
  4427.       register unsigned length = strlen (sys_dir);
  4428.  
  4429.       if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
  4430.     {
  4431.       if (searchptr->c_system_include_path)
  4432.         return 2;
  4433.       else
  4434.         return 1;
  4435.     }
  4436.     }
  4437.   return 0;
  4438. }
  4439.  
  4440. /* The file_name_map structure holds a mapping of file names for a
  4441.    particular directory.  This mapping is read from the file named
  4442.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  4443.    map filenames on a file system with severe filename restrictions,
  4444.    such as DOS.  The format of the file name map file is just a series
  4445.    of lines with two tokens on each line.  The first token is the name
  4446.    to map, and the second token is the actual name to use.  */
  4447.  
  4448. struct file_name_map
  4449. {
  4450.   struct file_name_map *map_next;
  4451.   char *map_from;
  4452.   char *map_to;
  4453. };
  4454.  
  4455. #define FILE_NAME_MAP_FILE "header.gcc"
  4456.  
  4457. /* Read a space delimited string of unlimited length from a stdio
  4458.    file.  */
  4459.  
  4460. static char *
  4461. read_filename_string (ch, f)
  4462.      int ch;
  4463.      FILE *f;
  4464. {
  4465.   char *alloc, *set;
  4466.   int len;
  4467.  
  4468.   len = 20;
  4469.   set = alloc = xmalloc (len + 1);
  4470.   if (! is_space[ch])
  4471.     {
  4472.       *set++ = ch;
  4473.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  4474.     {
  4475.       if (set - alloc == len)
  4476.         {
  4477.           len *= 2;
  4478.           alloc = xrealloc (alloc, len + 1);
  4479.           set = alloc + len / 2;
  4480.         }
  4481.       *set++ = ch;
  4482.     }
  4483.     }
  4484.   *set = '\0';
  4485.   ungetc (ch, f);
  4486.   return alloc;
  4487. }
  4488.  
  4489. /* Read the file name map file for DIRNAME.  */
  4490.  
  4491. static struct file_name_map *
  4492. read_name_map (dirname)
  4493.      char *dirname;
  4494. {
  4495.   /* This structure holds a linked list of file name maps, one per
  4496.      directory.  */
  4497.   struct file_name_map_list
  4498.     {
  4499.       struct file_name_map_list *map_list_next;
  4500.       char *map_list_name;
  4501.       struct file_name_map *map_list_map;
  4502.     };
  4503.   static struct file_name_map_list *map_list;
  4504.   register struct file_name_map_list *map_list_ptr;
  4505.   char *name;
  4506.   FILE *f;
  4507.  
  4508.   for (map_list_ptr = map_list; map_list_ptr;
  4509.        map_list_ptr = map_list_ptr->map_list_next)
  4510.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  4511.       return map_list_ptr->map_list_map;
  4512.  
  4513.   map_list_ptr = ((struct file_name_map_list *)
  4514.           xmalloc (sizeof (struct file_name_map_list)));
  4515.   map_list_ptr->map_list_name = savestring (dirname);
  4516.   map_list_ptr->map_list_map = NULL;
  4517.  
  4518.   name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
  4519.   strcpy (name, dirname);
  4520.   if (*dirname)
  4521.     strcat (name, "/");
  4522.   strcat (name, FILE_NAME_MAP_FILE);
  4523.   f = fopen (name, "r");
  4524.   if (!f)
  4525.     map_list_ptr->map_list_map = NULL;
  4526.   else
  4527.     {
  4528.       int ch;
  4529.       int dirlen = strlen (dirname);
  4530.  
  4531.       while ((ch = getc (f)) != EOF)
  4532.     {
  4533.       char *from, *to;
  4534.       struct file_name_map *ptr;
  4535.  
  4536.       if (is_space[ch])
  4537.         continue;
  4538.       from = read_filename_string (ch, f);
  4539.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  4540.         ;
  4541.       to = read_filename_string (ch, f);
  4542.  
  4543.       ptr = ((struct file_name_map *)
  4544.          xmalloc (sizeof (struct file_name_map)));
  4545.       ptr->map_from = from;
  4546.  
  4547.       /* Make the real filename absolute.  */
  4548.       if (*to == '/')
  4549.         ptr->map_to = to;
  4550.       else
  4551.         {
  4552.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  4553.           strcpy (ptr->map_to, dirname);
  4554.           ptr->map_to[dirlen] = '/';
  4555.           strcpy (ptr->map_to + dirlen + 1, to);
  4556.           free (to);
  4557.         }          
  4558.  
  4559.       ptr->map_next = map_list_ptr->map_list_map;
  4560.       map_list_ptr->map_list_map = ptr;
  4561.  
  4562.       while ((ch = getc (f)) != '\n')
  4563.         if (ch == EOF)
  4564.           break;
  4565.     }
  4566.       fclose (f);
  4567.     }
  4568.   
  4569.   map_list_ptr->map_list_next = map_list;
  4570.   map_list = map_list_ptr;
  4571.  
  4572.   return map_list_ptr->map_list_map;
  4573. }  
  4574.  
  4575. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  4576.    being tried from the include file search path.  This function maps
  4577.    filenames on file systems based on information read by
  4578.    read_name_map.  */
  4579.  
  4580. static int
  4581. open_include_file (filename, searchptr)
  4582.      char *filename;
  4583.      struct file_name_list *searchptr;
  4584. {
  4585.   register struct file_name_map *map;
  4586.   register char *from;
  4587.   char *p, *dir;
  4588.  
  4589.   if (searchptr && ! searchptr->got_name_map)
  4590.     {
  4591.       searchptr->name_map = read_name_map (searchptr->fname
  4592.                        ? searchptr->fname : ".");
  4593.       searchptr->got_name_map = 1;
  4594.     }
  4595.  
  4596.   /* First check the mapping for the directory we are using.  */
  4597.   if (searchptr && searchptr->name_map)
  4598.     {
  4599.       from = filename;
  4600.       if (searchptr->fname)
  4601.     from += strlen (searchptr->fname) + 1;
  4602.       for (map = searchptr->name_map; map; map = map->map_next)
  4603.     {
  4604.       if (! strcmp (map->map_from, from))
  4605.         {
  4606.           /* Found a match.  */
  4607.           return open (map->map_to, O_RDONLY, 0666);
  4608.         }
  4609.     }
  4610.     }
  4611.  
  4612.   /* Try to find a mapping file for the particular directory we are
  4613.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  4614.      in /usr/include/header.gcc and look up types.h in
  4615.      /usr/include/sys/header.gcc.  */
  4616.   p = rindex (filename, '/');
  4617.   if (! p)
  4618.     p = filename;
  4619.   if (searchptr
  4620.       && searchptr->fname
  4621.       && strlen (searchptr->fname) == p - filename
  4622.       && ! strncmp (searchptr->fname, filename, p - filename))
  4623.     {
  4624.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  4625.       return open (filename, O_RDONLY, 0666);
  4626.     }
  4627.  
  4628.   if (p == filename)
  4629.     {
  4630.       dir = ".";
  4631.       from = filename;
  4632.     }
  4633.   else
  4634.     {
  4635.       dir = (char *) alloca (p - filename + 1);
  4636.       bcopy (filename, dir, p - filename);
  4637.       dir[p - filename] = '\0';
  4638.       from = p + 1;
  4639.     }
  4640.   for (map = read_name_map (dir); map; map = map->map_next)
  4641.     if (! strcmp (map->map_from, from))
  4642.       return open (map->map_to, O_RDONLY, 0666);
  4643.  
  4644.   return open (filename, O_RDONLY, 0666);
  4645. }
  4646.  
  4647. /* Process the contents of include file FNAME, already open on descriptor F,
  4648.    with output to OP.
  4649.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  4650.    "system" include directories (as decided by the `is_system_include'
  4651.    function above).
  4652.    DIRPTR is the link in the dir path through which this file was found,
  4653.    or 0 if the file name was absolute.  */
  4654.  
  4655. static void
  4656. finclude (f, fname, op, system_header_p, dirptr)
  4657.      int f;
  4658.      char *fname;
  4659.      FILE_BUF *op;
  4660.      int system_header_p;
  4661.      struct file_name_list *dirptr;
  4662. {
  4663.   int st_mode;
  4664.   long st_size;
  4665.   long i;
  4666.   FILE_BUF *fp;            /* For input stack frame */
  4667.   int missing_newline = 0;
  4668.  
  4669.   CHECK_DEPTH (return;);
  4670.  
  4671.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  4672.     {
  4673.       perror_with_name (fname);
  4674.       close (f);
  4675.       return;
  4676.     }
  4677.  
  4678.   fp = &instack[indepth + 1];
  4679.   bzero ((char *) fp, sizeof (FILE_BUF));
  4680.   fp->nominal_fname = fp->fname = fname;
  4681.   fp->length = 0;
  4682.   fp->lineno = 1;
  4683.   fp->if_stack = if_stack;
  4684.   fp->system_header_p = system_header_p;
  4685.   fp->dir = dirptr;
  4686.  
  4687.   if (S_ISREG (st_mode)) {
  4688.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  4689.     fp->bufp = fp->buf;
  4690.  
  4691.     /* Read the file contents, knowing that st_size is an upper bound
  4692.        on the number of bytes we can read.  */
  4693.     fp->length = safe_read (f, fp->buf, st_size);
  4694.     if (fp->length < 0) goto nope;
  4695.   }
  4696.   else if (S_ISDIR (st_mode)) {
  4697.     error ("directory `%s' specified in #include", fname);
  4698.     close (f);
  4699.     return;
  4700.   } else {
  4701.     /* Cannot count its file size before reading.
  4702.        First read the entire file into heap and
  4703.        copy them into buffer on stack. */
  4704.  
  4705.     int bsize = 2000;
  4706.  
  4707.     st_size = 0;
  4708.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  4709.  
  4710.     for (;;) {
  4711.       i = safe_read (f, fp->buf + st_size, bsize - st_size);
  4712.       if (i < 0)
  4713.     goto nope;      /* error! */
  4714.       st_size += i;
  4715.       if (st_size != bsize)
  4716.     break;    /* End of file */
  4717.       bsize *= 2;
  4718.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  4719.     }
  4720.     fp->bufp = fp->buf;
  4721.     fp->length = st_size;
  4722.   }
  4723.  
  4724.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  4725.       /* Backslash-newline at end is not good enough.  */
  4726.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  4727.     fp->buf[fp->length++] = '\n';
  4728.     missing_newline = 1;
  4729.   }
  4730.   fp->buf[fp->length] = '\0';
  4731.  
  4732.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  4733.   close (f);
  4734.  
  4735.   /* Must do this before calling trigraph_pcp, so that the correct file name
  4736.      will be printed in warning messages.  */
  4737.  
  4738.   indepth++;
  4739.   input_file_stack_tick++;
  4740.  
  4741.   if (!no_trigraphs)
  4742.     trigraph_pcp (fp);
  4743.  
  4744.   output_line_command (fp, op, 0, enter_file);
  4745.   rescan (op, 0);
  4746.  
  4747.   if (missing_newline)
  4748.     fp->lineno--;
  4749.  
  4750.   if (pedantic && missing_newline)
  4751.     pedwarn ("file does not end in newline");
  4752.  
  4753.   indepth--;
  4754.   input_file_stack_tick++;
  4755.   output_line_command (&instack[indepth], op, 0, leave_file);
  4756.   free (fp->buf);
  4757.   return;
  4758.  
  4759.  nope:
  4760.  
  4761.   perror_with_name (fname);
  4762.   close (f);
  4763.   free (fp->buf);
  4764. }
  4765.  
  4766. /* Record that inclusion of the file named FILE
  4767.    should be controlled by the macro named MACRO_NAME.
  4768.    This means that trying to include the file again
  4769.    will do something if that macro is defined.  */
  4770.  
  4771. static void
  4772. record_control_macro (file, macro_name)
  4773.      char *file;
  4774.      U_CHAR *macro_name;
  4775. {
  4776.   struct file_name_list *new;
  4777.  
  4778.   for (new = all_include_files; new; new = new->next) {
  4779.     if (!strcmp (new->fname, file)) {
  4780.       new->control_macro = macro_name;
  4781.       return;
  4782.     }
  4783.   }
  4784.  
  4785.   /* If the file is not in all_include_files, something's wrong.  */
  4786.   abort ();
  4787. }
  4788.  
  4789. /* Maintain and search list of included files, for #import.  */
  4790.  
  4791. #define IMPORT_HASH_SIZE 31
  4792.  
  4793. struct import_file {
  4794.   char *name;
  4795.   ino_t inode;
  4796.   dev_t dev;
  4797.   struct import_file *next;
  4798. };
  4799.  
  4800. /* Hash table of files already included with #include or #import.  */
  4801.  
  4802. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  4803.  
  4804. /* Hash a file name for import_hash_table.  */
  4805.  
  4806. static int 
  4807. import_hash (f)
  4808.      char *f;
  4809. {
  4810.   int val = 0;
  4811.  
  4812.   while (*f) val += *f++;
  4813.   return (val%IMPORT_HASH_SIZE);
  4814. }
  4815.  
  4816. /* Search for file FILENAME in import_hash_table.
  4817.    Return -2 if found, either a matching name or a matching inode.
  4818.    Otherwise, open the file and return a file descriptor if successful
  4819.    or -1 if unsuccessful.  */
  4820.  
  4821. static int
  4822. lookup_import (filename, searchptr)
  4823.      char *filename;
  4824.      struct file_name_list *searchptr;
  4825. {
  4826.   struct import_file *i;
  4827.   int h;
  4828.   int hashval;
  4829.   struct stat sb;
  4830.   int fd;
  4831.  
  4832.   hashval = import_hash (filename);
  4833.  
  4834.   /* Attempt to find file in list of already included files */
  4835.   i = import_hash_table[hashval];
  4836.  
  4837.   while (i) {
  4838.     if (!strcmp (filename, i->name))
  4839.       return -2;        /* return found */
  4840.     i = i->next;
  4841.   }
  4842.   /* Open it and try a match on inode/dev */
  4843.   fd = open_include_file (filename, searchptr);
  4844.   if (fd < 0)
  4845.     return fd;
  4846.   fstat (fd, &sb);
  4847.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  4848.     i = import_hash_table[h];
  4849.     while (i) {
  4850.       /* Compare the inode and the device.
  4851.      Supposedly on some systems the inode is not a scalar.  */
  4852.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  4853.       && i->dev == sb.st_dev) {
  4854.         close (fd);
  4855.         return -2;        /* return found */
  4856.       }
  4857.       i = i->next;
  4858.     }
  4859.   }
  4860.   return fd;            /* Not found, return open file */
  4861. }
  4862.  
  4863. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  4864.  
  4865. static void
  4866. add_import (fd, fname)
  4867.      int fd;
  4868.      char *fname;
  4869. {
  4870.   struct import_file *i;
  4871.   int hashval;
  4872.   struct stat sb;
  4873.  
  4874.   hashval = import_hash (fname);
  4875.   fstat (fd, &sb);
  4876.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  4877.   i->name = (char *)xmalloc (strlen (fname)+1);
  4878.   strcpy (i->name, fname);
  4879.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  4880.   i->dev = sb.st_dev;
  4881.   i->next = import_hash_table[hashval];
  4882.   import_hash_table[hashval] = i;
  4883. }
  4884.  
  4885. /* Load the specified precompiled header into core, and verify its
  4886.    preconditions.  PCF indicates the file descriptor to read, which must
  4887.    be a regular file.  FNAME indicates the file name of the original 
  4888.    header.  *LIMIT will be set to an address one past the end of the file.
  4889.    If the preconditions of the file are not satisfied, the buffer is 
  4890.    freed and we return 0.  If the preconditions are satisfied, return
  4891.    the address of the buffer following the preconditions.  The buffer, in
  4892.    this case, should never be freed because various pieces of it will
  4893.    be referred to until all precompiled strings are output at the end of
  4894.    the run.
  4895. */
  4896. static char *
  4897. check_precompiled (pcf, fname, limit)
  4898.      int pcf;
  4899.      char *fname;
  4900.      char **limit;
  4901. {
  4902.   int st_mode;
  4903.   long st_size;
  4904.   int length = 0;
  4905.   char *buf;
  4906.   char *cp;
  4907.  
  4908.   if (pcp_outfile)
  4909.     return 0;
  4910.   
  4911.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  4912.     return 0;
  4913.  
  4914.   if (S_ISREG (st_mode))
  4915.     {
  4916.       buf = xmalloc (st_size + 2);
  4917.       length = safe_read (pcf, buf, st_size);
  4918.       if (length < 0)
  4919.     goto nope;
  4920.     }
  4921.   else
  4922.     abort ();
  4923.     
  4924.   if (length > 0 && buf[length-1] != '\n')
  4925.     buf[length++] = '\n';
  4926.   buf[length] = '\0';
  4927.   
  4928.   *limit = buf + length;
  4929.  
  4930.   /* File is in core.  Check the preconditions. */
  4931.   if (!check_preconditions (buf))
  4932.     goto nope;
  4933.   for (cp = buf; *cp; cp++)
  4934.     ;
  4935. #ifdef DEBUG_PCP
  4936.   fprintf (stderr, "Using preinclude %s\n", fname);
  4937. #endif
  4938.   return cp + 1;
  4939.  
  4940.  nope:
  4941. #ifdef DEBUG_PCP
  4942.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  4943. #endif
  4944.   free (buf);
  4945.   return 0;
  4946. }
  4947.  
  4948. /* PREC (null terminated) points to the preconditions of a
  4949.    precompiled header.  These are a series of #define and #undef
  4950.    lines which must match the current contents of the hash
  4951.    table.  */
  4952. static int 
  4953. check_preconditions (prec)
  4954.      char *prec;
  4955. {
  4956.   MACRODEF mdef;
  4957.   char *lineend;
  4958.   
  4959.   while (*prec) {
  4960.     lineend = (char *) index (prec, '\n');
  4961.     
  4962.     if (*prec++ != '#') {
  4963.       error ("Bad format encountered while reading precompiled file");
  4964.       return 0;
  4965.     }
  4966.     if (!strncmp (prec, "define", 6)) {
  4967.       HASHNODE *hp;
  4968.       
  4969.       prec += 6;
  4970.       mdef = create_definition (prec, lineend, NULL_PTR);
  4971.  
  4972.       if (mdef.defn == 0)
  4973.     abort ();
  4974.       
  4975.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  4976.       || (hp->type != T_MACRO && hp->type != T_CONST)
  4977.       || (hp->type == T_MACRO
  4978.           && !compare_defs (mdef.defn, hp->value.defn)
  4979.           && (mdef.defn->length != 2
  4980.           || mdef.defn->expansion[0] != '\n'
  4981.           || mdef.defn->expansion[1] != ' ')))
  4982.     return 0;
  4983.     } else if (!strncmp (prec, "undef", 5)) {
  4984.       char *name;
  4985.       int len;
  4986.       
  4987.       prec += 5;
  4988.       while (is_hor_space[(U_CHAR) *prec])
  4989.     prec++;
  4990.       name = prec;
  4991.       while (is_idchar[(U_CHAR) *prec])
  4992.     prec++;
  4993.       len = prec - name;
  4994.       
  4995.       if (lookup (name, len, -1))
  4996.     return 0;
  4997.     } else {
  4998.       error ("Bad format encountered while reading precompiled file");
  4999.       return 0;
  5000.     }
  5001.     prec = lineend + 1;
  5002.   }
  5003.   /* They all passed successfully */
  5004.   return 1;
  5005. }
  5006.  
  5007. /* Process the main body of a precompiled file.  BUF points to the
  5008.    string section of the file, following the preconditions.  LIMIT is one
  5009.    character past the end.  NAME is the name of the file being read
  5010.    in.  OP is the main output buffer */
  5011. static void
  5012. pcfinclude (buf, limit, name, op)
  5013.      U_CHAR *buf, *limit, *name;
  5014.      FILE_BUF *op;
  5015. {
  5016.   FILE_BUF tmpbuf;
  5017.   int nstrings;
  5018.   U_CHAR *cp = buf;
  5019.  
  5020.   /* First in the file comes 4 bytes indicating the number of strings, */
  5021.   /* in network byte order. (MSB first).  */
  5022.   nstrings = *cp++;
  5023.   nstrings = (nstrings << 8) | *cp++;
  5024.   nstrings = (nstrings << 8) | *cp++;
  5025.   nstrings = (nstrings << 8) | *cp++;
  5026.   
  5027.   /* Looping over each string... */
  5028.   while (nstrings--) {
  5029.     U_CHAR *string_start;
  5030.     U_CHAR *endofthiskey;
  5031.     STRINGDEF *str;
  5032.     int nkeys;
  5033.     
  5034.     /* Each string starts with a STRINGDEF structure (str), followed */
  5035.     /* by the text of the string (string_start) */
  5036.  
  5037.     /* First skip to a longword boundary */
  5038.     /* ??? Why a 4-byte boundary?  On all machines? */
  5039.     /* NOTE: This works correctly even if HOST_WIDE_INT
  5040.        is narrower than a pointer.
  5041.        Do not try risky measures here to get another type to use!
  5042.        Do not include stddef.h--it will fail!  */
  5043.     if ((HOST_WIDE_INT) cp & 3)
  5044.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  5045.     
  5046.     /* Now get the string. */
  5047.     str = (STRINGDEF *) cp;
  5048.     string_start = cp += sizeof (STRINGDEF);
  5049.     
  5050.     for (; *cp; cp++)        /* skip the string */
  5051.       ;
  5052.     
  5053.     /* We need to macro expand the string here to ensure that the
  5054.        proper definition environment is in place.  If it were only
  5055.        expanded when we find out it is needed, macros necessary for
  5056.        its proper expansion might have had their definitions changed. */
  5057.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  5058.     /* Lineno is already set in the precompiled file */
  5059.     str->contents = tmpbuf.buf;
  5060.     str->len = tmpbuf.length;
  5061.     str->writeflag = 0;
  5062.     str->filename = name;
  5063.     str->output_mark = outbuf.bufp - outbuf.buf;
  5064.     
  5065.     str->chain = 0;
  5066.     *stringlist_tailp = str;
  5067.     stringlist_tailp = &str->chain;
  5068.     
  5069.     /* Next comes a fourbyte number indicating the number of keys */
  5070.     /* for this string. */
  5071.     nkeys = *cp++;
  5072.     nkeys = (nkeys << 8) | *cp++;
  5073.     nkeys = (nkeys << 8) | *cp++;
  5074.     nkeys = (nkeys << 8) | *cp++;
  5075.  
  5076.     /* If this number is -1, then the string is mandatory. */
  5077.     if (nkeys == -1)
  5078.       str->writeflag = 1;
  5079.     else
  5080.       /* Otherwise, for each key, */
  5081.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  5082.     KEYDEF *kp = (KEYDEF *) cp;
  5083.     HASHNODE *hp;
  5084.     
  5085.     /* It starts with a KEYDEF structure */
  5086.     cp += sizeof (KEYDEF);
  5087.     
  5088.     /* Find the end of the key.  At the end of this for loop we
  5089.        advance CP to the start of the next key using this variable. */
  5090.     endofthiskey = cp + strlen (cp);
  5091.     kp->str = str;
  5092.     
  5093.     /* Expand the key, and enter it into the hash table. */
  5094.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  5095.     tmpbuf.bufp = tmpbuf.buf;
  5096.     
  5097.     while (is_hor_space[*tmpbuf.bufp])
  5098.       tmpbuf.bufp++;
  5099.     if (!is_idstart[*tmpbuf.bufp]
  5100.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  5101.       str->writeflag = 1;
  5102.       continue;
  5103.     }
  5104.         
  5105.     hp = lookup (tmpbuf.bufp, -1, -1);
  5106.     if (hp == NULL) {
  5107.       kp->chain = 0;
  5108.       install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
  5109.     }
  5110.     else if (hp->type == T_PCSTRING) {
  5111.       kp->chain = hp->value.keydef;
  5112.       hp->value.keydef = kp;
  5113.     }
  5114.     else
  5115.       str->writeflag = 1;
  5116.       }
  5117.   }
  5118.   /* This output_line_command serves to switch us back to the current
  5119.      input file in case some of these strings get output (which will 
  5120.      result in line commands for the header file being output). */
  5121.   output_line_command (&instack[indepth], op, 0, enter_file);
  5122. }
  5123.  
  5124. /* Called from rescan when it hits a key for strings.  Mark them all */
  5125.  /* used and clean up. */
  5126. static void
  5127. pcstring_used (hp)
  5128.      HASHNODE *hp;
  5129. {
  5130.   KEYDEF *kp;
  5131.   
  5132.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  5133.     kp->str->writeflag = 1;
  5134.   delete_macro (hp);
  5135. }
  5136.  
  5137. /* Write the output, interspersing precompiled strings in their */
  5138.  /* appropriate places. */
  5139. static void
  5140. write_output ()
  5141. {
  5142.   STRINGDEF *next_string;
  5143.   U_CHAR *cur_buf_loc;
  5144.   int line_command_len = 80;
  5145.   char *line_command = xmalloc (line_command_len);
  5146.   int len;
  5147.  
  5148.   /* In each run through the loop, either cur_buf_loc == */
  5149.   /* next_string_loc, in which case we print a series of strings, or */
  5150.   /* it is less than next_string_loc, in which case we write some of */
  5151.   /* the buffer. */
  5152.   cur_buf_loc = outbuf.buf; 
  5153.   next_string = stringlist;
  5154.   
  5155.   while (cur_buf_loc < outbuf.bufp || next_string) {
  5156.     if (next_string
  5157.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  5158.       if (next_string->writeflag) {
  5159.     len = 4 * strlen (next_string->filename) + 32;
  5160.     while (len > line_command_len)
  5161.       line_command = xrealloc (line_command, 
  5162.                    line_command_len *= 2);
  5163.     sprintf (line_command, "\n# %d ", next_string->lineno);
  5164.     strcpy (quote_string (line_command + strlen (line_command),
  5165.                       next_string->filename),
  5166.         "\n");
  5167.     safe_write (fileno (stdout), line_command, strlen (line_command));
  5168.     safe_write (fileno (stdout), next_string->contents, next_string->len);
  5169.       }          
  5170.       next_string = next_string->chain;
  5171.     }
  5172.     else {
  5173.       len = (next_string
  5174.          ? (next_string->output_mark 
  5175.         - (cur_buf_loc - outbuf.buf))
  5176.          : outbuf.bufp - cur_buf_loc);
  5177.       
  5178.       safe_write (fileno (stdout), cur_buf_loc, len);
  5179.       cur_buf_loc += len;
  5180.     }
  5181.   }
  5182.   free (line_command);
  5183. }
  5184.  
  5185. /* Pass a directive through to the output file.
  5186.    BUF points to the contents of the directive, as a contiguous string.
  5187.    LIMIT points to the first character past the end of the directive.
  5188.    KEYWORD is the keyword-table entry for the directive.  */
  5189.  
  5190. static void
  5191. pass_thru_directive (buf, limit, op, keyword)
  5192.      U_CHAR *buf, *limit;
  5193.      FILE_BUF *op;
  5194.      struct directive *keyword;
  5195. {
  5196.   register unsigned keyword_length = keyword->length;
  5197.  
  5198.   check_expand (op, 1 + keyword_length + (limit - buf));
  5199.   *op->bufp++ = '#';
  5200.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  5201.   op->bufp += keyword_length;
  5202.   if (limit != buf && buf[0] != ' ')
  5203.     *op->bufp++ = ' ';
  5204.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  5205.   op->bufp += (limit - buf);
  5206. #if 0
  5207.   *op->bufp++ = '\n';
  5208.   /* Count the line we have just made in the output,
  5209.      to get in sync properly.  */
  5210.   op->lineno++;
  5211. #endif
  5212. }
  5213.  
  5214. /* The arglist structure is built by do_define to tell
  5215.    collect_definition where the argument names begin.  That
  5216.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  5217.    would contain pointers to the strings x, y, and z.
  5218.    Collect_definition would then build a DEFINITION node,
  5219.    with reflist nodes pointing to the places x, y, and z had
  5220.    appeared.  So the arglist is just convenience data passed
  5221.    between these two routines.  It is not kept around after
  5222.    the current #define has been processed and entered into the
  5223.    hash table. */
  5224.  
  5225. struct arglist {
  5226.   struct arglist *next;
  5227.   U_CHAR *name;
  5228.   int length;
  5229.   int argno;
  5230.   char rest_args;
  5231. };
  5232.  
  5233. /* Create a DEFINITION node from a #define directive.  Arguments are 
  5234.    as for do_define. */
  5235. static MACRODEF
  5236. create_definition (buf, limit, op)
  5237.      U_CHAR *buf, *limit;
  5238.      FILE_BUF *op;
  5239. {
  5240.   U_CHAR *bp;            /* temp ptr into input buffer */
  5241.   U_CHAR *symname;        /* remember where symbol name starts */
  5242.   int sym_length;        /* and how long it is */
  5243.   int line = instack[indepth].lineno;
  5244.   char *file = instack[indepth].nominal_fname;
  5245.   int rest_args = 0;
  5246.  
  5247.   DEFINITION *defn;
  5248.   int arglengths = 0;        /* Accumulate lengths of arg names
  5249.                    plus number of args.  */
  5250.   MACRODEF mdef;
  5251.  
  5252.   bp = buf;
  5253.  
  5254.   while (is_hor_space[*bp])
  5255.     bp++;
  5256.  
  5257.   symname = bp;            /* remember where it starts */
  5258.   sym_length = check_macro_name (bp, "macro");
  5259.   bp += sym_length;
  5260.  
  5261.   /* Lossage will occur if identifiers or control keywords are broken
  5262.      across lines using backslash.  This is not the right place to take
  5263.      care of that. */
  5264.  
  5265.   if (*bp == '(') {
  5266.     struct arglist *arg_ptrs = NULL;
  5267.     int argno = 0;
  5268.  
  5269.     bp++;            /* skip '(' */
  5270.     SKIP_WHITE_SPACE (bp);
  5271.  
  5272.     /* Loop over macro argument names.  */
  5273.     while (*bp != ')') {
  5274.       struct arglist *temp;
  5275.  
  5276.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  5277.       temp->name = bp;
  5278.       temp->next = arg_ptrs;
  5279.       temp->argno = argno++;
  5280.       temp->rest_args = 0;
  5281.       arg_ptrs = temp;
  5282.  
  5283.       if (rest_args)
  5284.     pedwarn ("another parameter follows `%s'",
  5285.          rest_extension);
  5286.  
  5287.       if (!is_idstart[*bp])
  5288.     pedwarn ("invalid character in macro parameter name");
  5289.       
  5290.       /* Find the end of the arg name.  */
  5291.       while (is_idchar[*bp]) {
  5292.     bp++;
  5293.     /* do we have a "special" rest-args extension here? */
  5294.     if (limit - bp > REST_EXTENSION_LENGTH &&
  5295.         strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  5296.       rest_args = 1;
  5297.       temp->rest_args = 1;
  5298.       break;
  5299.     }
  5300.       }
  5301.       temp->length = bp - temp->name;
  5302.       if (rest_args == 1)
  5303.     bp += REST_EXTENSION_LENGTH;
  5304.       arglengths += temp->length + 2;
  5305.       SKIP_WHITE_SPACE (bp);
  5306.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  5307.     error ("badly punctuated parameter list in `#define'");
  5308.     goto nope;
  5309.       }
  5310.       if (*bp == ',') {
  5311.     bp++;
  5312.     SKIP_WHITE_SPACE (bp);
  5313.       }
  5314.       if (bp >= limit) {
  5315.     error ("unterminated parameter list in `#define'");
  5316.     goto nope;
  5317.       }
  5318.       {
  5319.     struct arglist *otemp;
  5320.  
  5321.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  5322.       if (temp->length == otemp->length &&
  5323.         strncmp (temp->name, otemp->name, temp->length) == 0) {
  5324.           U_CHAR *name;
  5325.  
  5326.           name = (U_CHAR *) alloca (temp->length + 1);
  5327.           (void) strncpy (name, temp->name, temp->length);
  5328.           name[temp->length] = '\0';
  5329.           error ("duplicate argument name `%s' in `#define'", name);
  5330.           goto nope;
  5331.       }
  5332.       }
  5333.     }
  5334.  
  5335.     ++bp;            /* skip paren */
  5336.     /* Skip spaces and tabs if any.  */
  5337.     while (bp < limit && (*bp == ' ' || *bp == '\t'))
  5338.       ++bp;
  5339.     /* now everything from bp before limit is the definition. */
  5340.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  5341.     defn->rest_args = rest_args;
  5342.  
  5343.     /* Now set defn->args.argnames to the result of concatenating
  5344.        the argument names in reverse order
  5345.        with comma-space between them.  */
  5346.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  5347.     {
  5348.       struct arglist *temp;
  5349.       int i = 0;
  5350.       for (temp = arg_ptrs; temp; temp = temp->next) {
  5351.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  5352.     i += temp->length;
  5353.     if (temp->next != 0) {
  5354.       defn->args.argnames[i++] = ',';
  5355.       defn->args.argnames[i++] = ' ';
  5356.     }
  5357.       }
  5358.       defn->args.argnames[i] = 0;
  5359.     }
  5360.   } else {
  5361.     /* Simple expansion or empty definition.  */
  5362.  
  5363.     /* Skip spaces and tabs if any.  */
  5364.     while (bp < limit && (*bp == ' ' || *bp == '\t'))
  5365.       ++bp;
  5366.     /* Now everything from bp before limit is the definition. */
  5367.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  5368.     defn->args.argnames = (U_CHAR *) "";
  5369.   }
  5370.  
  5371.   defn->line = line;
  5372.   defn->file = file;
  5373.  
  5374.   /* OP is null if this is a predefinition */
  5375.   defn->predefined = !op;
  5376.   mdef.defn = defn;
  5377.   mdef.symnam = symname;
  5378.   mdef.symlen = sym_length;
  5379.  
  5380.   return mdef;
  5381.  
  5382.  nope:
  5383.   mdef.defn = 0;
  5384.   return mdef;
  5385. }
  5386.  
  5387. /* Process a #define command.
  5388. BUF points to the contents of the #define command, as a contiguous string.
  5389. LIMIT points to the first character past the end of the definition.
  5390. KEYWORD is the keyword-table entry for #define.  */
  5391.  
  5392. static int
  5393. do_define (buf, limit, op, keyword)
  5394.      U_CHAR *buf, *limit;
  5395.      FILE_BUF *op;
  5396.      struct directive *keyword;
  5397. {
  5398.   int hashcode;
  5399.   MACRODEF mdef;
  5400.  
  5401.   /* If this is a precompiler run (with -pcp) pass thru #define commands.  */
  5402.   if (pcp_outfile && op)
  5403.     pass_thru_directive (buf, limit, op, keyword);
  5404.  
  5405.   mdef = create_definition (buf, limit, op);
  5406.   if (mdef.defn == 0)
  5407.     goto nope;
  5408.  
  5409.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  5410.  
  5411.   {
  5412.     HASHNODE *hp;
  5413.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  5414.       int ok = 0;
  5415.       /* Redefining a precompiled key is ok.  */
  5416.       if (hp->type == T_PCSTRING)
  5417.     ok = 1;
  5418.       /* Redefining a macro is ok if the definitions are the same.  */
  5419.       else if (hp->type == T_MACRO)
  5420.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  5421.       /* Redefining a constant is ok with -D.  */
  5422.       else if (hp->type == T_CONST)
  5423.         ok = ! done_initializing;
  5424.       /* Print the warning if it's not ok.  */
  5425.       if (!ok) {
  5426.     U_CHAR *msg;        /* what pain... */
  5427.  
  5428.         /* If we are passing through #define and #undef directives, do
  5429.        that for this re-definition now.  */
  5430.         if (debug_output && op)
  5431.       pass_thru_directive (buf, limit, op, keyword);
  5432.  
  5433.     msg = (U_CHAR *) alloca (mdef.symlen + 22);
  5434.     *msg = '`';
  5435.     bcopy ((char *) mdef.symnam, (char *) (msg + 1), mdef.symlen);
  5436.     strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
  5437.     pedwarn (msg);
  5438.     if (hp->type == T_MACRO)
  5439.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  5440.                       "this is the location of the previous definition");
  5441.       }
  5442.       /* Replace the old definition.  */
  5443.       hp->type = T_MACRO;
  5444.       hp->value.defn = mdef.defn;
  5445.     } else {
  5446.       /* If we are passing through #define and #undef directives, do
  5447.      that for this new definition now.  */
  5448.       if (debug_output && op)
  5449.     pass_thru_directive (buf, limit, op, keyword);
  5450.       install (mdef.symnam, mdef.symlen, T_MACRO, 0,
  5451.            (char *) mdef.defn, hashcode);
  5452.     }
  5453.   }
  5454.  
  5455.   return 0;
  5456.  
  5457. nope:
  5458.  
  5459.   return 1;
  5460. }
  5461.  
  5462. /* Check a purported macro name SYMNAME, and yield its length.
  5463.    USAGE is the kind of name this is intended for.  */
  5464.  
  5465. static int
  5466. check_macro_name (symname, usage)
  5467.      U_CHAR *symname;
  5468.      char *usage;
  5469. {
  5470.   U_CHAR *p;
  5471.   int sym_length;
  5472.  
  5473.   for (p = symname; is_idchar[*p]; p++)
  5474.     ;
  5475.   sym_length = p - symname;
  5476.   if (sym_length == 0)
  5477.     error ("invalid %s name", usage);
  5478.   else if (!is_idstart[*symname]) {
  5479.     U_CHAR *msg;            /* what pain... */
  5480.     msg = (U_CHAR *) alloca (sym_length + 1);
  5481.     bcopy ((char *) symname, (char *) msg, sym_length);
  5482.     msg[sym_length] = 0;
  5483.     error ("invalid %s name `%s'", usage, msg);
  5484.   } else {
  5485.     if (! strncmp (symname, "defined", 7) && sym_length == 7)
  5486.       error ("invalid %s name `defined'", usage);
  5487.   }
  5488.   return sym_length;
  5489. }
  5490.  
  5491. /*
  5492.  * return zero if two DEFINITIONs are isomorphic
  5493.  */
  5494. static int
  5495. compare_defs (d1, d2)
  5496.      DEFINITION *d1, *d2;
  5497. {
  5498.   register struct reflist *a1, *a2;
  5499.   register U_CHAR *p1 = d1->expansion;
  5500.   register U_CHAR *p2 = d2->expansion;
  5501.   int first = 1;
  5502.  
  5503.   if (d1->nargs != d2->nargs)
  5504.     return 1;
  5505.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  5506.     return 1;
  5507.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  5508.        a1 = a1->next, a2 = a2->next) {
  5509.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  5510.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  5511.     || a1->argno != a2->argno
  5512.     || a1->stringify != a2->stringify
  5513.     || a1->raw_before != a2->raw_before
  5514.     || a1->raw_after != a2->raw_after)
  5515.       return 1;
  5516.     first = 0;
  5517.     p1 += a1->nchars;
  5518.     p2 += a2->nchars;
  5519.   }
  5520.   if (a1 != a2)
  5521.     return 1;
  5522.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  5523.              p2, d2->length - (p2 - d2->expansion), 1))
  5524.     return 1;
  5525.   return 0;
  5526. }
  5527.  
  5528. /* Return 1 if two parts of two macro definitions are effectively different.
  5529.    One of the parts starts at BEG1 and has LEN1 chars;
  5530.    the other has LEN2 chars at BEG2.
  5531.    Any sequence of whitespace matches any other sequence of whitespace.
  5532.    FIRST means these parts are the first of a macro definition;
  5533.     so ignore leading whitespace entirely.
  5534.    LAST means these parts are the last of a macro definition;
  5535.     so ignore trailing whitespace entirely.  */
  5536.  
  5537. static int
  5538. comp_def_part (first, beg1, len1, beg2, len2, last)
  5539.      int first;
  5540.      U_CHAR *beg1, *beg2;
  5541.      int len1, len2;
  5542.      int last;
  5543. {
  5544.   register U_CHAR *end1 = beg1 + len1;
  5545.   register U_CHAR *end2 = beg2 + len2;
  5546.   if (first) {
  5547.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  5548.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  5549.   }
  5550.   if (last) {
  5551.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  5552.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  5553.   }
  5554.   while (beg1 != end1 && beg2 != end2) {
  5555.     if (is_space[*beg1] && is_space[*beg2]) {
  5556.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  5557.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  5558.     } else if (*beg1 == *beg2) {
  5559.       beg1++; beg2++;
  5560.     } else break;
  5561.   }
  5562.   return (beg1 != end1) || (beg2 != end2);
  5563. }
  5564.  
  5565. /* Read a replacement list for a macro with parameters.
  5566.    Build the DEFINITION structure.
  5567.    Reads characters of text starting at BUF until END.
  5568.    ARGLIST specifies the formal parameters to look for
  5569.    in the text of the definition; NARGS is the number of args
  5570.    in that list, or -1 for a macro name that wants no argument list.
  5571.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  5572.    and NAMELEN is its length in characters.
  5573.    
  5574. Note that comments and backslash-newlines have already been deleted
  5575. from the argument.  */
  5576.  
  5577. /* Leading and trailing Space, Tab, etc. are converted to markers
  5578.    Newline Space, Newline Tab, etc.
  5579.    Newline Space makes a space in the final output
  5580.    but is discarded if stringified.  (Newline Tab is similar but
  5581.    makes a Tab instead.)
  5582.  
  5583.    If there is no trailing whitespace, a Newline Space is added at the end
  5584.    to prevent concatenation that would be contrary to the standard.  */
  5585.  
  5586. static DEFINITION *
  5587. collect_expansion (buf, end, nargs, arglist)
  5588.      U_CHAR *buf, *end;
  5589.      int nargs;
  5590.      struct arglist *arglist;
  5591. {
  5592.   DEFINITION *defn;
  5593.   register U_CHAR *p, *limit, *lastp, *exp_p;
  5594.   struct reflist *endpat = NULL;
  5595.   /* Pointer to first nonspace after last ## seen.  */
  5596.   U_CHAR *concat = 0;
  5597.   /* Pointer to first nonspace after last single-# seen.  */
  5598.   U_CHAR *stringify = 0;
  5599.   int maxsize;
  5600.   int expected_delimiter = '\0';
  5601.  
  5602.   /* Scan thru the replacement list, ignoring comments and quoted
  5603.      strings, picking up on the macro calls.  It does a linear search
  5604.      thru the arg list on every potential symbol.  Profiling might say
  5605.      that something smarter should happen. */
  5606.  
  5607.   if (end < buf)
  5608.     abort ();
  5609.  
  5610.   /* Find the beginning of the trailing whitespace.  */
  5611.   /* Find end of leading whitespace.  */
  5612.   limit = end;
  5613.   p = buf;
  5614.   while (p < limit && is_space[limit[-1]]) limit--;
  5615.   while (p < limit && is_space[*p]) p++;
  5616.  
  5617.   /* Allocate space for the text in the macro definition.
  5618.      Leading and trailing whitespace chars need 2 bytes each.
  5619.      Each other input char may or may not need 1 byte,
  5620.      so this is an upper bound.
  5621.      The extra 2 are for invented trailing newline-marker and final null.  */
  5622.   maxsize = (sizeof (DEFINITION)
  5623.          + 2 * (end - limit) + 2 * (p - buf)
  5624.          + (limit - p) + 3);
  5625.   defn = (DEFINITION *) xcalloc (1, maxsize);
  5626.  
  5627.   defn->nargs = nargs;
  5628.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  5629.   lastp = exp_p;
  5630.  
  5631.   p = buf;
  5632.  
  5633.   /* Convert leading whitespace to Newline-markers.  */
  5634.   while (p < limit && is_space[*p]) {
  5635.     *exp_p++ = '\n';
  5636.     *exp_p++ = *p++;
  5637.   }
  5638.  
  5639.   if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
  5640.     error ("`##' at start of macro definition");
  5641.     p += 2;
  5642.   }
  5643.  
  5644.   /* Process the main body of the definition.  */
  5645.   while (p < limit) {
  5646.     int skipped_arg = 0;
  5647.     register U_CHAR c = *p++;
  5648.  
  5649.     *exp_p++ = c;
  5650.  
  5651.     if (!traditional) {
  5652.       switch (c) {
  5653.       case '\'':
  5654.       case '\"':
  5655.         if (expected_delimiter != '\0') {
  5656.           if (c == expected_delimiter)
  5657.             expected_delimiter = '\0';
  5658.         } else
  5659.           expected_delimiter = c;
  5660.     break;
  5661.  
  5662.     /* Special hack: if a \# is written in the #define
  5663.        include a # in the definition.  This is useless for C code
  5664.        but useful for preprocessing other things.  */
  5665.  
  5666.       case '\\':
  5667.     /* \# quotes a # even outside of strings.  */
  5668.     if (p < limit && *p == '#' && !expected_delimiter) {
  5669.       exp_p--;
  5670.       *exp_p++ = *p++;
  5671.     } else if (p < limit && expected_delimiter) {
  5672.       /* In a string, backslash goes through
  5673.          and makes next char ordinary.  */
  5674.       *exp_p++ = *p++;
  5675.     }
  5676.     break;
  5677.  
  5678.       case '#':
  5679.     /* # is ordinary inside a string.  */
  5680.     if (expected_delimiter)
  5681.       break;
  5682.     if (p < limit && *p == '#') {
  5683.       /* ##: concatenate preceding and following tokens.  */
  5684.       /* Take out the first #, discard preceding whitespace.  */
  5685.       exp_p--;
  5686.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  5687.         --exp_p;
  5688.       /* Skip the second #.  */
  5689.       p++;
  5690.       /* Discard following whitespace.  */
  5691.       SKIP_WHITE_SPACE (p);
  5692.       concat = p;
  5693.       if (p == limit)
  5694.         error ("`##' at end of macro definition");
  5695.     } else if (nargs >= 0) {
  5696.       /* Single #: stringify following argument ref.
  5697.          Don't leave the # in the expansion.  */
  5698.       exp_p--;
  5699.       SKIP_WHITE_SPACE (p);
  5700.       if (p == limit || ! is_idstart[*p])
  5701.         error ("`#' operator is not followed by a macro argument name");
  5702.       else
  5703.         stringify = p;
  5704.     }
  5705.     break;
  5706.       }
  5707.     } else {
  5708.       /* In -traditional mode, recognize arguments inside strings and
  5709.      and character constants, and ignore special properties of #.
  5710.      Arguments inside strings are considered "stringified", but no
  5711.      extra quote marks are supplied.  */
  5712.       switch (c) {
  5713.       case '\'':
  5714.       case '\"':
  5715.     if (expected_delimiter != '\0') {
  5716.       if (c == expected_delimiter)
  5717.         expected_delimiter = '\0';
  5718.     } else
  5719.       expected_delimiter = c;
  5720.     break;
  5721.  
  5722.       case '\\':
  5723.     /* Backslash quotes delimiters and itself, but not macro args.  */
  5724.     if (expected_delimiter != 0 && p < limit
  5725.         && (*p == expected_delimiter || *p == '\\')) {
  5726.       *exp_p++ = *p++;
  5727.       continue;
  5728.     }
  5729.     break;
  5730.  
  5731.       case '/':
  5732.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  5733.       break;
  5734.     if (*p == '*') {
  5735.       /* If we find a comment that wasn't removed by handle_directive,
  5736.          this must be -traditional.  So replace the comment with
  5737.          nothing at all.  */
  5738.       exp_p--;
  5739.       p += 1;
  5740.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  5741.         p++;
  5742. #if 0
  5743.       /* Mark this as a concatenation-point, as if it had been ##.  */
  5744.       concat = p;
  5745. #endif
  5746.     }
  5747.     break;
  5748.       }
  5749.     }
  5750.  
  5751.     /* Handle the start of a symbol.  */
  5752.     if (is_idchar[c] && nargs > 0) {
  5753.       U_CHAR *id_beg = p - 1;
  5754.       int id_len;
  5755.  
  5756.       --exp_p;
  5757.       while (p != limit && is_idchar[*p]) p++;
  5758.       id_len = p - id_beg;
  5759.  
  5760.       if (is_idstart[c]) {
  5761.     register struct arglist *arg;
  5762.  
  5763.     for (arg = arglist; arg != NULL; arg = arg->next) {
  5764.       struct reflist *tpat;
  5765.  
  5766.       if (arg->name[0] == c
  5767.           && arg->length == id_len
  5768.           && strncmp (arg->name, id_beg, id_len) == 0) {
  5769.         if (expected_delimiter && warn_stringify) {
  5770.           if (traditional) {
  5771.         warning ("macro argument `%.*s' is stringified.",
  5772.              id_len, arg->name);
  5773.           } else {
  5774.         warning ("macro arg `%.*s' would be stringified with -traditional.",
  5775.              id_len, arg->name);
  5776.           }
  5777.         }
  5778.         /* If ANSI, don't actually substitute inside a string.  */
  5779.         if (!traditional && expected_delimiter)
  5780.           break;
  5781.         /* make a pat node for this arg and append it to the end of
  5782.            the pat list */
  5783.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  5784.         tpat->next = NULL;
  5785.         tpat->raw_before = concat == id_beg;
  5786.         tpat->raw_after = 0;
  5787.         tpat->rest_args = arg->rest_args;
  5788.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  5789.                    : stringify == id_beg);
  5790.  
  5791.         if (endpat == NULL)
  5792.           defn->pattern = tpat;
  5793.         else
  5794.           endpat->next = tpat;
  5795.         endpat = tpat;
  5796.  
  5797.         tpat->argno = arg->argno;
  5798.         tpat->nchars = exp_p - lastp;
  5799.         {
  5800.           register U_CHAR *p1 = p;
  5801.           SKIP_WHITE_SPACE (p1);
  5802.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  5803.         tpat->raw_after = 1;
  5804.         }
  5805.         lastp = exp_p;    /* place to start copying from next time */
  5806.         skipped_arg = 1;
  5807.         break;
  5808.       }
  5809.     }
  5810.       }
  5811.  
  5812.       /* If this was not a macro arg, copy it into the expansion.  */
  5813.       if (! skipped_arg) {
  5814.     register U_CHAR *lim1 = p;
  5815.     p = id_beg;
  5816.     while (p != lim1)
  5817.       *exp_p++ = *p++;
  5818.     if (stringify == id_beg)
  5819.       error ("`#' operator should be followed by a macro argument name");
  5820.       }
  5821.     }
  5822.   }
  5823.  
  5824.   if (!traditional && expected_delimiter == 0) {
  5825.     /* There is no trailing whitespace, so invent some in ANSI mode.
  5826.        But not if "inside a string" (which in ANSI mode
  5827.        happens only for -D option).  */
  5828.     *exp_p++ = '\n';
  5829.     *exp_p++ = ' ';
  5830.   }
  5831.  
  5832.   *exp_p = '\0';
  5833.  
  5834.   defn->length = exp_p - defn->expansion;
  5835.  
  5836.   /* Crash now if we overrun the allocated size.  */
  5837.   if (defn->length + 1 > maxsize)
  5838.     abort ();
  5839.  
  5840. #if 0
  5841. /* This isn't worth the time it takes.  */
  5842.   /* give back excess storage */
  5843.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  5844. #endif
  5845.  
  5846.   return defn;
  5847. }
  5848.  
  5849. static int
  5850. do_assert (buf, limit, op, keyword)
  5851.      U_CHAR *buf, *limit;
  5852.      FILE_BUF *op;
  5853.      struct directive *keyword;
  5854. {
  5855.   U_CHAR *bp;            /* temp ptr into input buffer */
  5856.   U_CHAR *symname;        /* remember where symbol name starts */
  5857.   int sym_length;        /* and how long it is */
  5858.   struct arglist *tokens = NULL;
  5859.  
  5860.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  5861.     pedwarn ("ANSI C does not allow `#assert'");
  5862.  
  5863.   bp = buf;
  5864.  
  5865.   while (is_hor_space[*bp])
  5866.     bp++;
  5867.  
  5868.   symname = bp;            /* remember where it starts */
  5869.   sym_length = check_macro_name (bp, "assertion");
  5870.   bp += sym_length;
  5871.   /* #define doesn't do this, but we should.  */
  5872.   SKIP_WHITE_SPACE (bp);
  5873.  
  5874.   /* Lossage will occur if identifiers or control tokens are broken
  5875.      across lines using backslash.  This is not the right place to take
  5876.      care of that. */
  5877.  
  5878.   if (*bp != '(') {
  5879.     error ("missing token-sequence in `#assert'");
  5880.     return 1;
  5881.   }
  5882.  
  5883.   {
  5884.     int error_flag = 0;
  5885.  
  5886.     bp++;            /* skip '(' */
  5887.     SKIP_WHITE_SPACE (bp);
  5888.  
  5889.     tokens = read_token_list (&bp, limit, &error_flag);
  5890.     if (error_flag)
  5891.       return 1;
  5892.     if (tokens == 0) {
  5893.       error ("empty token-sequence in `#assert'");
  5894.       return 1;
  5895.     }
  5896.  
  5897.     ++bp;            /* skip paren */
  5898.     SKIP_WHITE_SPACE (bp);
  5899.   }
  5900.  
  5901.   /* If this name isn't already an assertion name, make it one.
  5902.      Error if it was already in use in some other way.  */
  5903.  
  5904.   {
  5905.     ASSERTION_HASHNODE *hp;
  5906.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  5907.     struct tokenlist_list *value
  5908.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  5909.  
  5910.     hp = assertion_lookup (symname, sym_length, hashcode);
  5911.     if (hp == NULL) {
  5912.       if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
  5913.     error ("`defined' redefined as assertion");
  5914.       hp = assertion_install (symname, sym_length, hashcode);
  5915.     }
  5916.  
  5917.     /* Add the spec'd token-sequence to the list of such.  */
  5918.     value->tokens = tokens;
  5919.     value->next = hp->value;
  5920.     hp->value = value;
  5921.   }
  5922.  
  5923.   return 0;
  5924. }
  5925.  
  5926. static int
  5927. do_unassert (buf, limit, op, keyword)
  5928.      U_CHAR *buf, *limit;
  5929.      FILE_BUF *op;
  5930.      struct directive *keyword;
  5931. {
  5932.   U_CHAR *bp;            /* temp ptr into input buffer */
  5933.   U_CHAR *symname;        /* remember where symbol name starts */
  5934.   int sym_length;        /* and how long it is */
  5935.  
  5936.   struct arglist *tokens = NULL;
  5937.   int tokens_specified = 0;
  5938.  
  5939.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  5940.     pedwarn ("ANSI C does not allow `#unassert'");
  5941.  
  5942.   bp = buf;
  5943.  
  5944.   while (is_hor_space[*bp])
  5945.     bp++;
  5946.  
  5947.   symname = bp;            /* remember where it starts */
  5948.   sym_length = check_macro_name (bp, "assertion");
  5949.   bp += sym_length;
  5950.   /* #define doesn't do this, but we should.  */
  5951.   SKIP_WHITE_SPACE (bp);
  5952.  
  5953.   /* Lossage will occur if identifiers or control tokens are broken
  5954.      across lines using backslash.  This is not the right place to take
  5955.      care of that. */
  5956.  
  5957.   if (*bp == '(') {
  5958.     int error_flag = 0;
  5959.  
  5960.     bp++;            /* skip '(' */
  5961.     SKIP_WHITE_SPACE (bp);
  5962.  
  5963.     tokens = read_token_list (&bp, limit, &error_flag);
  5964.     if (error_flag)
  5965.       return 1;
  5966.     if (tokens == 0) {
  5967.       error ("empty token list in `#unassert'");
  5968.       return 1;
  5969.     }
  5970.  
  5971.     tokens_specified = 1;
  5972.  
  5973.     ++bp;            /* skip paren */
  5974.     SKIP_WHITE_SPACE (bp);
  5975.   }
  5976.  
  5977.   {
  5978.     ASSERTION_HASHNODE *hp;
  5979.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  5980.     struct tokenlist_list *tail, *prev;
  5981.  
  5982.     hp = assertion_lookup (symname, sym_length, hashcode);
  5983.     if (hp == NULL)
  5984.       return 1;
  5985.  
  5986.     /* If no token list was specified, then eliminate this assertion
  5987.        entirely.  */
  5988.     if (! tokens_specified) {
  5989.       struct tokenlist_list *next;
  5990.       for (tail = hp->value; tail; tail = next) {
  5991.     next = tail->next;
  5992.     free_token_list (tail->tokens);
  5993.     free (tail);
  5994.       }
  5995.       delete_assertion (hp);
  5996.     } else {
  5997.       /* If a list of tokens was given, then delete any matching list.  */
  5998.  
  5999.       tail = hp->value;
  6000.       prev = 0;
  6001.       while (tail) {
  6002.     struct tokenlist_list *next = tail->next;
  6003.     if (compare_token_lists (tail->tokens, tokens)) {
  6004.       if (prev)
  6005.         prev->next = next;
  6006.       else
  6007.         hp->value = tail->next;
  6008.       free_token_list (tail->tokens);
  6009.       free (tail);
  6010.     } else {
  6011.       prev = tail;
  6012.     }
  6013.     tail = next;
  6014.       }
  6015.     }
  6016.   }
  6017.  
  6018.   return 0;
  6019. }
  6020.  
  6021. /* Test whether there is an assertion named NAME
  6022.    and optionally whether it has an asserted token list TOKENS.
  6023.    NAME is not null terminated; its length is SYM_LENGTH.
  6024.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  6025.  
  6026. int
  6027. check_assertion (name, sym_length, tokens_specified, tokens)
  6028.      U_CHAR *name;
  6029.      int sym_length;
  6030.      int tokens_specified;
  6031.      struct arglist *tokens;
  6032. {
  6033.   ASSERTION_HASHNODE *hp;
  6034.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  6035.  
  6036.   if (pedantic && !instack[indepth].system_header_p)
  6037.     pedwarn ("ANSI C does not allow testing assertions");
  6038.  
  6039.   hp = assertion_lookup (name, sym_length, hashcode);
  6040.   if (hp == NULL)
  6041.     /* It is not an assertion; just return false.  */
  6042.     return 0;
  6043.  
  6044.   /* If no token list was specified, then value is 1.  */
  6045.   if (! tokens_specified)
  6046.     return 1;
  6047.  
  6048.   {
  6049.     struct tokenlist_list *tail;
  6050.  
  6051.     tail = hp->value;
  6052.  
  6053.     /* If a list of tokens was given,
  6054.        then succeed if the assertion records a matching list.  */
  6055.  
  6056.     while (tail) {
  6057.       if (compare_token_lists (tail->tokens, tokens))
  6058.     return 1;
  6059.       tail = tail->next;
  6060.     }
  6061.  
  6062.     /* Fail if the assertion has no matching list.  */
  6063.     return 0;
  6064.   }
  6065. }
  6066.  
  6067. /* Compare two lists of tokens for equality including order of tokens.  */
  6068.  
  6069. static int
  6070. compare_token_lists (l1, l2)
  6071.      struct arglist *l1, *l2;
  6072. {
  6073.   while (l1 && l2) {
  6074.     if (l1->length != l2->length)
  6075.       return 0;
  6076.     if (strncmp (l1->name, l2->name, l1->length))
  6077.       return 0;
  6078.     l1 = l1->next;
  6079.     l2 = l2->next;
  6080.   }
  6081.  
  6082.   /* Succeed if both lists end at the same time.  */
  6083.   return l1 == l2;
  6084. }
  6085.  
  6086. /* Read a space-separated list of tokens ending in a close parenthesis.
  6087.    Return a list of strings, in the order they were written.
  6088.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  6089.    Parse the text starting at *BPP, and update *BPP.
  6090.    Don't parse beyond LIMIT.  */
  6091.  
  6092. static struct arglist *
  6093. read_token_list (bpp, limit, error_flag)
  6094.      U_CHAR **bpp;
  6095.      U_CHAR *limit;
  6096.      int *error_flag;
  6097. {
  6098.   struct arglist *token_ptrs = 0;
  6099.   U_CHAR *bp = *bpp;
  6100.   int depth = 1;
  6101.  
  6102.   *error_flag = 0;
  6103.  
  6104.   /* Loop over the assertion value tokens.  */
  6105.   while (depth > 0) {
  6106.     struct arglist *temp;
  6107.     int eofp = 0;
  6108.     U_CHAR *beg = bp;
  6109.  
  6110.     /* Find the end of the token.  */
  6111.     if (*bp == '(') {
  6112.       bp++;
  6113.       depth++;
  6114.     } else if (*bp == ')') {
  6115.       depth--;
  6116.       if (depth == 0)
  6117.     break;
  6118.       bp++;
  6119.     } else if (*bp == '"' || *bp == '\'')
  6120.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  6121.     else
  6122.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  6123.          && *bp != '"' && *bp != '\'' && bp != limit)
  6124.     bp++;
  6125.  
  6126.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  6127.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  6128.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  6129.     temp->name[bp - beg] = 0;
  6130.     temp->next = token_ptrs;
  6131.     token_ptrs = temp;
  6132.     temp->length = bp - beg;
  6133.  
  6134.     SKIP_WHITE_SPACE (bp);
  6135.  
  6136.     if (bp >= limit) {
  6137.       error ("unterminated token sequence in `#assert' or `#unassert'");
  6138.       *error_flag = -1;
  6139.       return 0;
  6140.     }
  6141.   }
  6142.   *bpp = bp;
  6143.  
  6144.   /* We accumulated the names in reverse order.
  6145.      Now reverse them to get the proper order.  */
  6146.   {
  6147.     register struct arglist *prev = 0, *this, *next;
  6148.     for (this = token_ptrs; this; this = next) {
  6149.       next = this->next;
  6150.       this->next = prev;
  6151.       prev = this;
  6152.     }
  6153.     return prev;
  6154.   }
  6155. }
  6156.  
  6157. static void
  6158. free_token_list (tokens)
  6159.      struct arglist *tokens;
  6160. {
  6161.   while (tokens) {
  6162.     struct arglist *next = tokens->next;
  6163.     free (tokens->name);
  6164.     free (tokens);
  6165.     tokens = next;
  6166.   }
  6167. }
  6168.  
  6169. /*
  6170.  * Install a name in the assertion hash table.
  6171.  *
  6172.  * If LEN is >= 0, it is the length of the name.
  6173.  * Otherwise, compute the length by scanning the entire name.
  6174.  *
  6175.  * If HASH is >= 0, it is the precomputed hash code.
  6176.  * Otherwise, compute the hash code.
  6177.  */
  6178. static ASSERTION_HASHNODE *
  6179. assertion_install (name, len, hash)
  6180.      U_CHAR *name;
  6181.      int len;
  6182.      int hash;
  6183. {
  6184.   register ASSERTION_HASHNODE *hp;
  6185.   register int i, bucket;
  6186.   register U_CHAR *p, *q;
  6187.  
  6188.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  6189.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  6190.   bucket = hash;
  6191.   hp->bucket_hdr = &assertion_hashtab[bucket];
  6192.   hp->next = assertion_hashtab[bucket];
  6193.   assertion_hashtab[bucket] = hp;
  6194.   hp->prev = NULL;
  6195.   if (hp->next != NULL)
  6196.     hp->next->prev = hp;
  6197.   hp->length = len;
  6198.   hp->value = 0;
  6199.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  6200.   p = hp->name;
  6201.   q = name;
  6202.   for (i = 0; i < len; i++)
  6203.     *p++ = *q++;
  6204.   hp->name[len] = 0;
  6205.   return hp;
  6206. }
  6207.  
  6208. /*
  6209.  * find the most recent hash node for name name (ending with first
  6210.  * non-identifier char) installed by install
  6211.  *
  6212.  * If LEN is >= 0, it is the length of the name.
  6213.  * Otherwise, compute the length by scanning the entire name.
  6214.  *
  6215.  * If HASH is >= 0, it is the precomputed hash code.
  6216.  * Otherwise, compute the hash code.
  6217.  */
  6218. static ASSERTION_HASHNODE *
  6219. assertion_lookup (name, len, hash)
  6220.      U_CHAR *name;
  6221.      int len;
  6222.      int hash;
  6223. {
  6224.   register ASSERTION_HASHNODE *bucket;
  6225.  
  6226.   bucket = assertion_hashtab[hash];
  6227.   while (bucket) {
  6228.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  6229.       return bucket;
  6230.     bucket = bucket->next;
  6231.   }
  6232.   return NULL;
  6233. }
  6234.  
  6235. static void
  6236. delete_assertion (hp)
  6237.      ASSERTION_HASHNODE *hp;
  6238. {
  6239.  
  6240.   if (hp->prev != NULL)
  6241.     hp->prev->next = hp->next;
  6242.   if (hp->next != NULL)
  6243.     hp->next->prev = hp->prev;
  6244.  
  6245.   /* make sure that the bucket chain header that
  6246.      the deleted guy was on points to the right thing afterwards. */
  6247.   if (hp == *hp->bucket_hdr)
  6248.     *hp->bucket_hdr = hp->next;
  6249.  
  6250.   free (hp);
  6251. }
  6252.  
  6253. /*
  6254.  * interpret #line command.  Remembers previously seen fnames
  6255.  * in its very own hash table.
  6256.  */
  6257. #define FNAME_HASHSIZE 37
  6258.  
  6259. static int
  6260. do_line (buf, limit, op, keyword)
  6261.      U_CHAR *buf, *limit;
  6262.      FILE_BUF *op;
  6263.      struct directive *keyword;
  6264. {
  6265.   register U_CHAR *bp;
  6266.   FILE_BUF *ip = &instack[indepth];
  6267.   FILE_BUF tem;
  6268.   int new_lineno;
  6269.   enum file_change_code file_change = same_file;
  6270.  
  6271.   /* Expand any macros.  */
  6272.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  6273.  
  6274.   /* Point to macroexpanded line, which is null-terminated now.  */
  6275.   bp = tem.buf;
  6276.   SKIP_WHITE_SPACE (bp);
  6277.  
  6278.   if (!isdigit (*bp)) {
  6279.     error ("invalid format `#line' command");
  6280.     return 0;
  6281.   }
  6282.  
  6283.   /* The Newline at the end of this line remains to be processed.
  6284.      To put the next line at the specified line number,
  6285.      we must store a line number now that is one less.  */
  6286.   new_lineno = atoi (bp) - 1;
  6287.  
  6288.   /* NEW_LINENO is one less than the actual line number here.  */
  6289.   if (pedantic && new_lineno < 0)
  6290.     pedwarn ("line number out of range in `#line' command");
  6291.  
  6292.   /* skip over the line number.  */
  6293.   while (isdigit (*bp))
  6294.     bp++;
  6295.  
  6296. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  6297.   if (*bp && !is_space[*bp]) {
  6298.     error ("invalid format `#line' command");
  6299.     return;
  6300.   }
  6301. #endif
  6302.  
  6303.   SKIP_WHITE_SPACE (bp);
  6304.  
  6305.   if (*bp == '\"') {
  6306.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  6307.     HASHNODE *hp, **hash_bucket;
  6308.     U_CHAR *fname, *p;
  6309.     int fname_length;
  6310.  
  6311.     fname = ++bp;
  6312.  
  6313.     /* Turn the file name, which is a character string literal,
  6314.        into a null-terminated string.  Do this in place.  */
  6315.     p = bp;
  6316.     for (;;)
  6317.       switch ((*p++ = *bp++)) {
  6318.       case '\0':
  6319.     error ("invalid format `#line' command");
  6320.     return 0;
  6321.  
  6322.       case '\\':
  6323.     {
  6324.       char *bpc = (char *) bp;
  6325.       int c = parse_escape (&bpc);
  6326.       bp = (U_CHAR *) bpc;
  6327.       if (c < 0)
  6328.         p--;
  6329.       else
  6330.         p[-1] = c;
  6331.     }
  6332.     break;
  6333.  
  6334.       case '\"':
  6335.     p[-1] = 0;
  6336.     goto fname_done;
  6337.       }
  6338.   fname_done:
  6339.     fname_length = p - fname;
  6340.  
  6341.     SKIP_WHITE_SPACE (bp);
  6342.     if (*bp) {
  6343.       if (pedantic)
  6344.     pedwarn ("garbage at end of `#line' command");
  6345.       if (*bp == '1')
  6346.     file_change = enter_file;
  6347.       else if (*bp == '2')
  6348.     file_change = leave_file;
  6349.       else if (*bp == '3')
  6350.     ip->system_header_p = 1;
  6351.       else if (*bp == '4')
  6352.     ip->system_header_p = 2;
  6353.       else {
  6354.     error ("invalid format `#line' command");
  6355.     return 0;
  6356.       }
  6357.  
  6358.       bp++;
  6359.       SKIP_WHITE_SPACE (bp);
  6360.       if (*bp == '3') {
  6361.     ip->system_header_p = 1;
  6362.     bp++;
  6363.     SKIP_WHITE_SPACE (bp);
  6364.       }
  6365.       if (*bp == '4') {
  6366.     ip->system_header_p = 2;
  6367.     bp++;
  6368.     SKIP_WHITE_SPACE (bp);
  6369.       }
  6370.       if (*bp) {
  6371.     error ("invalid format `#line' command");
  6372.     return 0;
  6373.       }
  6374.     }
  6375.  
  6376.     hash_bucket =
  6377.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  6378.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  6379.       if (hp->length == fname_length &&
  6380.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  6381.     ip->nominal_fname = hp->value.cpval;
  6382.     break;
  6383.       }
  6384.     if (hp == 0) {
  6385.       /* Didn't find it; cons up a new one.  */
  6386.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  6387.       hp->next = *hash_bucket;
  6388.       *hash_bucket = hp;
  6389.  
  6390.       hp->length = fname_length;
  6391.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  6392.       bcopy (fname, hp->value.cpval, fname_length);
  6393.     }
  6394.   } else if (*bp) {
  6395.     error ("invalid format `#line' command");
  6396.     return 0;
  6397.   }
  6398.  
  6399.   ip->lineno = new_lineno;
  6400.   output_line_command (ip, op, 0, file_change);
  6401.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  6402.   return 0;
  6403. }
  6404.  
  6405. /*
  6406.  * remove the definition of a symbol from the symbol table.
  6407.  * according to un*x /lib/cpp, it is not an error to undef
  6408.  * something that has no definitions, so it isn't one here either.
  6409.  */
  6410.  
  6411. static int
  6412. do_undef (buf, limit, op, keyword)
  6413.      U_CHAR *buf, *limit;
  6414.      FILE_BUF *op;
  6415.      struct directive *keyword;
  6416. {
  6417.   int sym_length;
  6418.   HASHNODE *hp;
  6419.   U_CHAR *orig_buf = buf;
  6420.  
  6421.   /* If this is a precompiler run (with -pcp) pass thru #undef commands.  */
  6422.   if (pcp_outfile && op)
  6423.     pass_thru_directive (buf, limit, op, keyword);
  6424.  
  6425.   SKIP_WHITE_SPACE (buf);
  6426.   sym_length = check_macro_name (buf, "macro");
  6427.  
  6428.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  6429.     /* If we are generating additional info for debugging (with -g) we
  6430.        need to pass through all effective #undef commands.  */
  6431.     if (debug_output && op)
  6432.       pass_thru_directive (orig_buf, limit, op, keyword);
  6433.     if (hp->type != T_MACRO)
  6434.       warning ("undefining `%s'", hp->name);
  6435.     delete_macro (hp);
  6436.   }
  6437.  
  6438.   if (pedantic) {
  6439.     buf += sym_length;
  6440.     SKIP_WHITE_SPACE (buf);
  6441.     if (buf != limit)
  6442.       pedwarn ("garbage after `#undef' directive");
  6443.   }
  6444.   return 0;
  6445. }
  6446.  
  6447. /*
  6448.  * Report an error detected by the program we are processing.
  6449.  * Use the text of the line in the error message.
  6450.  * (We use error because it prints the filename & line#.)
  6451.  */
  6452.  
  6453. static int
  6454. do_error (buf, limit, op, keyword)
  6455.      U_CHAR *buf, *limit;
  6456.      FILE_BUF *op;
  6457.      struct directive *keyword;
  6458. {
  6459.   int length = limit - buf;
  6460.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6461.   bcopy ((char *) buf, (char *) copy, length);
  6462.   copy[length] = 0;
  6463.   SKIP_WHITE_SPACE (copy);
  6464.   error ("#error %s", copy);
  6465.   return 0;
  6466. }
  6467.  
  6468. /*
  6469.  * Report a warning detected by the program we are processing.
  6470.  * Use the text of the line in the warning message, then continue.
  6471.  * (We use error because it prints the filename & line#.)
  6472.  */
  6473.  
  6474. static int
  6475. do_warning (buf, limit, op, keyword)
  6476.      U_CHAR *buf, *limit;
  6477.      FILE_BUF *op;
  6478.      struct directive *keyword;
  6479. {
  6480.   int length = limit - buf;
  6481.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6482.   bcopy ((char *) buf, (char *) copy, length);
  6483.   copy[length] = 0;
  6484.   SKIP_WHITE_SPACE (copy);
  6485.   warning ("#warning %s", copy);
  6486.   return 0;
  6487. }
  6488.  
  6489. /* Remember the name of the current file being read from so that we can
  6490.    avoid ever including it again.  */
  6491.  
  6492. static int
  6493. do_once ()
  6494. {
  6495.   int i;
  6496.   FILE_BUF *ip = NULL;
  6497.  
  6498.   for (i = indepth; i >= 0; i--)
  6499.     if (instack[i].fname != NULL) {
  6500.       ip = &instack[i];
  6501.       break;
  6502.     }
  6503.  
  6504.   if (ip != NULL) {
  6505.     struct file_name_list *new;
  6506.     
  6507.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  6508.     new->next = dont_repeat_files;
  6509.     dont_repeat_files = new;
  6510.     new->fname = savestring (ip->fname);
  6511.     new->control_macro = 0;
  6512.     new->got_name_map = 0;
  6513.     new->c_system_include_path = 0;
  6514.   }
  6515.   return 0;
  6516. }
  6517.  
  6518. /* #ident has already been copied to the output file, so just ignore it.  */
  6519.  
  6520. static int
  6521. do_ident (buf, limit)
  6522.      U_CHAR *buf, *limit;
  6523. {
  6524.   FILE_BUF trybuf;
  6525.   int len;
  6526.   FILE_BUF *op = &outbuf;
  6527.  
  6528.   /* Allow #ident in system headers, since that's not user's fault.  */
  6529.   if (pedantic && !instack[indepth].system_header_p)
  6530.     pedwarn ("ANSI C does not allow `#ident'");
  6531.  
  6532.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  6533.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  6534.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  6535.   limit = buf + (trybuf.bufp - trybuf.buf);
  6536.   len = (limit - buf);
  6537.   free (trybuf.buf);
  6538.  
  6539.   /* Output directive name.  */
  6540.   check_expand (op, 8);
  6541.   bcopy ("#ident ", (char *) op->bufp, 7);
  6542.   op->bufp += 7;
  6543.  
  6544.   /* Output the expanded argument line.  */
  6545.   check_expand (op, len);
  6546.   bcopy ((char *) buf, (char *) op->bufp, len);
  6547.   op->bufp += len;
  6548.  
  6549.   return 0;
  6550. }
  6551.  
  6552. /* #pragma and its argument line have already been copied to the output file.
  6553.    Just check for some recognized pragmas that need validation here.  */
  6554.  
  6555. static int
  6556. do_pragma (buf, limit)
  6557.      U_CHAR *buf, *limit;
  6558. {
  6559.   while (*buf == ' ' || *buf == '\t')
  6560.     buf++;
  6561.   if (!strncmp (buf, "once", 4)) {
  6562.     /* Allow #pragma once in system headers, since that's not the user's
  6563.        fault.  */
  6564.     if (!instack[indepth].system_header_p)
  6565.       warning ("`#pragma once' is obsolete");
  6566.     do_once ();
  6567.   }
  6568.  
  6569.   if (!strncmp (buf, "implementation", 14)) {
  6570.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  6571.        been included yet.  */
  6572.     struct file_name_list *ptr;
  6573.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  6574.     SKIP_WHITE_SPACE (p);
  6575.     if (*p == '\n' || *p != '\"')
  6576.       return 0;
  6577.  
  6578.     fname = p + 1;
  6579.     if (p = (U_CHAR *) index (fname, '\"'))
  6580.       *p = '\0';
  6581.     
  6582.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  6583.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  6584.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  6585.       if (inc_fname && !strcmp (inc_fname, fname))
  6586.     warning ("`#pragma implementation' for `%s' appears after file is included",
  6587.          fname);
  6588.     }
  6589.   }
  6590.  
  6591.   return 0;
  6592. }
  6593.  
  6594. #if 0
  6595. /* This was a fun hack, but #pragma seems to start to be useful.
  6596.    By failing to recognize it, we pass it through unchanged to cc1.  */
  6597.  
  6598. /*
  6599.  * the behavior of the #pragma directive is implementation defined.
  6600.  * this implementation defines it as follows.
  6601.  */
  6602.  
  6603. static int
  6604. do_pragma ()
  6605. {
  6606.   close (0);
  6607.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  6608.     goto nope;
  6609.   close (1);
  6610.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  6611.     goto nope;
  6612.   execl ("/usr/games/hack", "#pragma", 0);
  6613.   execl ("/usr/games/rogue", "#pragma", 0);
  6614.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  6615.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  6616. nope:
  6617.   fatal ("You are in a maze of twisty compiler features, all different");
  6618. }
  6619. #endif
  6620.  
  6621. /* Just ignore #sccs, on systems where we define it at all.  */
  6622.  
  6623. static int
  6624. do_sccs ()
  6625. {
  6626.   if (pedantic)
  6627.     pedwarn ("ANSI C does not allow `#sccs'");
  6628.   return 0;
  6629. }
  6630.  
  6631. /*
  6632.  * handle #if command by
  6633.  *   1) inserting special `defined' keyword into the hash table
  6634.  *    that gets turned into 0 or 1 by special_symbol (thus,
  6635.  *    if the luser has a symbol called `defined' already, it won't
  6636.  *      work inside the #if command)
  6637.  *   2) rescan the input into a temporary output buffer
  6638.  *   3) pass the output buffer to the yacc parser and collect a value
  6639.  *   4) clean up the mess left from steps 1 and 2.
  6640.  *   5) call conditional_skip to skip til the next #endif (etc.),
  6641.  *      or not, depending on the value from step 3.
  6642.  */
  6643.  
  6644. static int
  6645. do_if (buf, limit, op, keyword)
  6646.      U_CHAR *buf, *limit;
  6647.      FILE_BUF *op;
  6648.      struct directive *keyword;
  6649. {
  6650.   int value;
  6651.   FILE_BUF *ip = &instack[indepth];
  6652.  
  6653.   value = eval_if_expression (buf, limit - buf);
  6654.   conditional_skip (ip, value == 0, T_IF, NULL_PTR);
  6655.   return 0;
  6656. }
  6657.  
  6658. /*
  6659.  * handle a #elif directive by not changing  if_stack  either.
  6660.  * see the comment above do_else.
  6661.  */
  6662.  
  6663. static int
  6664. do_elif (buf, limit, op, keyword)
  6665.      U_CHAR *buf, *limit;
  6666.      FILE_BUF *op;
  6667.      struct directive *keyword;
  6668. {
  6669.   int value;
  6670.   FILE_BUF *ip = &instack[indepth];
  6671.  
  6672.   if (if_stack == instack[indepth].if_stack) {
  6673.     error ("`#elif' not within a conditional");
  6674.     return 0;
  6675.   } else {
  6676.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  6677.       error ("`#elif' after `#else'");
  6678.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  6679.       if (if_stack->fname != NULL && ip->fname != NULL &&
  6680.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  6681.     fprintf (stderr, ", file %s", if_stack->fname);
  6682.       fprintf (stderr, ")\n");
  6683.     }
  6684.     if_stack->type = T_ELIF;
  6685.   }
  6686.  
  6687.   if (if_stack->if_succeeded)
  6688.     skip_if_group (ip, 0);
  6689.   else {
  6690.     value = eval_if_expression (buf, limit - buf);
  6691.     if (value == 0)
  6692.       skip_if_group (ip, 0);
  6693.     else {
  6694.       ++if_stack->if_succeeded;    /* continue processing input */
  6695.       output_line_command (ip, op, 1, same_file);
  6696.     }
  6697.   }
  6698.   return 0;
  6699. }
  6700.  
  6701. /*
  6702.  * evaluate a #if expression in BUF, of length LENGTH,
  6703.  * then parse the result as a C expression and return the value as an int.
  6704.  */
  6705. static int
  6706. eval_if_expression (buf, length)
  6707.      U_CHAR *buf;
  6708.      int length;
  6709. {
  6710.   FILE_BUF temp_obuf;
  6711.   HASHNODE *save_defined;
  6712.   int value;
  6713.  
  6714.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
  6715.   pcp_inside_if = 1;
  6716.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  6717.   pcp_inside_if = 0;
  6718.   delete_macro (save_defined);    /* clean up special symbol */
  6719.  
  6720.   value = parse_c_expression (temp_obuf.buf);
  6721.  
  6722.   free (temp_obuf.buf);
  6723.  
  6724.   return value;
  6725. }
  6726.  
  6727. /*
  6728.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  6729.  * then do or don't skip to the #endif/#else/#elif depending
  6730.  * on what directive is actually being processed.
  6731.  */
  6732.  
  6733. static int
  6734. do_xifdef (buf, limit, op, keyword)
  6735.      U_CHAR *buf, *limit;
  6736.      FILE_BUF *op;
  6737.      struct directive *keyword;
  6738. {
  6739.   int skip;
  6740.   FILE_BUF *ip = &instack[indepth];
  6741.   U_CHAR *end; 
  6742.   int start_of_file = 0;
  6743.   U_CHAR *control_macro = 0;
  6744.  
  6745.   /* Detect a #ifndef at start of file (not counting comments).  */
  6746.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  6747.     U_CHAR *p = ip->buf;
  6748.     while (p != directive_start) {
  6749.       U_CHAR c = *p++;
  6750.       if (is_space[c])
  6751.     ;
  6752.       else if (c == '/' && p != ip->bufp && *p == '*') {
  6753.     /* Skip this comment.  */
  6754.     int junk = 0;
  6755.     U_CHAR *save_bufp = ip->bufp;
  6756.     ip->bufp = p + 1;
  6757.     p = skip_to_end_of_comment (ip, &junk, 1);
  6758.     ip->bufp = save_bufp;
  6759.       } else {
  6760.     goto fail;
  6761.       }
  6762.     }
  6763.     /* If we get here, this conditional is the beginning of the file.  */
  6764.     start_of_file = 1;
  6765.   fail: ;
  6766.   }
  6767.  
  6768.   /* Discard leading and trailing whitespace.  */
  6769.   SKIP_WHITE_SPACE (buf);
  6770.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  6771.  
  6772.   /* Find the end of the identifier at the beginning.  */
  6773.   for (end = buf; is_idchar[*end]; end++);
  6774.  
  6775.   if (end == buf) {
  6776.     skip = (keyword->type == T_IFDEF);
  6777.     if (! traditional)
  6778.       pedwarn (end == limit ? "`#%s' with no argument"
  6779.            : "`#%s' argument starts with punctuation",
  6780.            keyword->name);
  6781.   } else {
  6782.     HASHNODE *hp;
  6783.  
  6784.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  6785.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  6786.     else if (end != limit && !traditional)
  6787.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  6788.  
  6789.     hp = lookup (buf, end-buf, -1);
  6790.  
  6791.     if (pcp_outfile) {
  6792.       /* Output a precondition for this macro.  */
  6793.       if (hp && hp->value.defn->predefined)
  6794.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  6795.       else {
  6796.     U_CHAR *cp = buf;
  6797.     fprintf (pcp_outfile, "#undef ");
  6798.     while (is_idchar[*cp]) /* Ick! */
  6799.       fputc (*cp++, pcp_outfile);
  6800.     putc ('\n', pcp_outfile);
  6801.       }
  6802.     }
  6803.  
  6804.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  6805.     if (start_of_file && !skip) {
  6806.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  6807.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  6808.       control_macro[end - buf] = 0;
  6809.     }
  6810.   }
  6811.   
  6812.   conditional_skip (ip, skip, T_IF, control_macro);
  6813.   return 0;
  6814. }
  6815.  
  6816. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  6817.    If this is a #ifndef starting at the beginning of a file,
  6818.    CONTROL_MACRO is the macro name tested by the #ifndef.
  6819.    Otherwise, CONTROL_MACRO is 0.  */
  6820.  
  6821. static void
  6822. conditional_skip (ip, skip, type, control_macro)
  6823.      FILE_BUF *ip;
  6824.      int skip;
  6825.      enum node_type type;
  6826.      U_CHAR *control_macro;
  6827. {
  6828.   IF_STACK_FRAME *temp;
  6829.  
  6830.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  6831.   temp->fname = ip->nominal_fname;
  6832.   temp->lineno = ip->lineno;
  6833.   temp->next = if_stack;
  6834.   temp->control_macro = control_macro;
  6835.   if_stack = temp;
  6836.  
  6837.   if_stack->type = type;
  6838.  
  6839.   if (skip != 0) {
  6840.     skip_if_group (ip, 0);
  6841.     return;
  6842.   } else {
  6843.     ++if_stack->if_succeeded;
  6844.     output_line_command (ip, &outbuf, 1, same_file);
  6845.   }
  6846. }
  6847.  
  6848. /*
  6849.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  6850.  * leaves input ptr at the sharp sign found.
  6851.  * If ANY is nonzero, return at next directive of any sort.
  6852.  */
  6853. static void
  6854. skip_if_group (ip, any)
  6855.      FILE_BUF *ip;
  6856.      int any;
  6857. {
  6858.   register U_CHAR *bp = ip->bufp, *cp;
  6859.   register U_CHAR *endb = ip->buf + ip->length;
  6860.   struct directive *kt;
  6861.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  6862.   U_CHAR *beg_of_line = bp;
  6863.   register int ident_length;
  6864.   U_CHAR *ident, *after_ident;
  6865.  
  6866.   while (bp < endb) {
  6867.     switch (*bp++) {
  6868.     case '/':            /* possible comment */
  6869.       if (*bp == '\\' && bp[1] == '\n')
  6870.     newline_fix (bp);
  6871.       if (*bp == '*'
  6872.       || (cplusplus_comments && *bp == '/')) {
  6873.     ip->bufp = ++bp;
  6874.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  6875.       }
  6876.       break;
  6877.     case '\"':
  6878.     case '\'':
  6879.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  6880.                    NULL_PTR, NULL_PTR);
  6881.       break;
  6882.     case '\\':
  6883.       /* Char after backslash loses its special meaning.  */
  6884.       if (bp < endb) {
  6885.     if (*bp == '\n')
  6886.       ++ip->lineno;        /* But do update the line-count.  */
  6887.     bp++;
  6888.       }
  6889.       break;
  6890.     case '\n':
  6891.       ++ip->lineno;
  6892.       beg_of_line = bp;
  6893.       break;
  6894.     case '#':
  6895.       ip->bufp = bp - 1;
  6896.  
  6897.       /* # keyword: a # must be first nonblank char on the line */
  6898.       if (beg_of_line == 0)
  6899.     break;
  6900.       /* Scan from start of line, skipping whitespace, comments
  6901.      and backslash-newlines, and see if we reach this #.
  6902.      If not, this # is not special.  */
  6903.       bp = beg_of_line;
  6904.       /* If -traditional, require # to be at beginning of line.  */
  6905.       if (!traditional)
  6906.     while (1) {
  6907.       if (is_hor_space[*bp])
  6908.         bp++;
  6909.       else if (*bp == '\\' && bp[1] == '\n')
  6910.         bp += 2;
  6911.       else if (*bp == '/' && bp[1] == '*') {
  6912.         bp += 2;
  6913.         while (!(*bp == '*' && bp[1] == '/'))
  6914.           bp++;
  6915.         bp += 2;
  6916.       }
  6917.       /* There is no point in trying to deal with C++ // comments here,
  6918.          because if there is one, then this # must be part of the
  6919.          comment and we would never reach here.  */
  6920.       else break;
  6921.     }
  6922.       if (bp != ip->bufp) {
  6923.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  6924.     break;
  6925.       }
  6926.  
  6927.       bp = ip->bufp + 1;    /* Point after the '#' */
  6928.  
  6929.       /* Skip whitespace and \-newline.  */
  6930.       while (1) {
  6931.     if (is_hor_space[*bp])
  6932.       bp++;
  6933.     else if (*bp == '\\' && bp[1] == '\n')
  6934.       bp += 2;
  6935.     else if (*bp == '/' && bp[1] == '*') {
  6936.       bp += 2;
  6937.       while (!(*bp == '*' && bp[1] == '/')) {
  6938.         if (*bp == '\n')
  6939.           ip->lineno++;
  6940.         bp++;
  6941.       }
  6942.       bp += 2;
  6943.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  6944.       bp += 2;
  6945.       while (bp[-1] == '\\' || *bp != '\n') {
  6946.         if (*bp == '\n')
  6947.           ip->lineno++;
  6948.         bp++;
  6949.       }
  6950.         }
  6951.     else break;
  6952.       }
  6953.  
  6954.       cp = bp;
  6955.  
  6956.       /* Now find end of directive name.
  6957.      If we encounter a backslash-newline, exchange it with any following
  6958.      symbol-constituents so that we end up with a contiguous name.  */
  6959.  
  6960.       while (1) {
  6961.     if (is_idchar[*bp])
  6962.       bp++;
  6963.     else {
  6964.       if (*bp == '\\' && bp[1] == '\n')
  6965.         name_newline_fix (bp);
  6966.       if (is_idchar[*bp])
  6967.         bp++;
  6968.       else break;
  6969.     }
  6970.       }
  6971.       ident_length = bp - cp;
  6972.       ident = cp;
  6973.       after_ident = bp;
  6974.  
  6975.       /* A line of just `#' becomes blank.  */
  6976.  
  6977.       if (ident_length == 0 && *after_ident == '\n') {
  6978.     continue;
  6979.       }
  6980.  
  6981.       if (ident_length == 0 || !is_idstart[*ident]) {
  6982.     U_CHAR *p = ident;
  6983.     while (is_idchar[*p]) {
  6984.       if (*p < '0' || *p > '9')
  6985.         break;
  6986.       p++;
  6987.     }
  6988.     /* Handle # followed by a line number.  */
  6989.     if (p != ident && !is_idchar[*p]) {
  6990.       if (pedantic)
  6991.         pedwarn ("`#' followed by integer");
  6992.       continue;
  6993.     }
  6994.  
  6995.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  6996.     if (p == ident) {
  6997.       while (*p == '#' || is_hor_space[*p]) p++;
  6998.       if (*p == '\n') {
  6999.         if (pedantic && !lang_asm)
  7000.           pedwarn ("invalid preprocessor directive");
  7001.         continue;
  7002.       }
  7003.     }
  7004.  
  7005.     if (!lang_asm && pedantic)
  7006.       pedwarn ("invalid preprocessor directive name");
  7007.     continue;
  7008.       }
  7009.  
  7010.       for (kt = directive_table; kt->length >= 0; kt++) {
  7011.     IF_STACK_FRAME *temp;
  7012.     if (ident_length == kt->length
  7013.         && strncmp (cp, kt->name, kt->length) == 0) {
  7014.       /* If we are asked to return on next directive, do so now.  */
  7015.       if (any)
  7016.         return;
  7017.  
  7018.       switch (kt->type) {
  7019.       case T_IF:
  7020.       case T_IFDEF:
  7021.       case T_IFNDEF:
  7022.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7023.         temp->next = if_stack;
  7024.         if_stack = temp;
  7025.         temp->lineno = ip->lineno;
  7026.         temp->fname = ip->nominal_fname;
  7027.         temp->type = kt->type;
  7028.         break;
  7029.       case T_ELSE:
  7030.       case T_ENDIF:
  7031.         if (pedantic && if_stack != save_if_stack)
  7032.           validate_else (bp);
  7033.       case T_ELIF:
  7034.         if (if_stack == instack[indepth].if_stack) {
  7035.           error ("`#%s' not within a conditional", kt->name);
  7036.           break;
  7037.         }
  7038.         else if (if_stack == save_if_stack)
  7039.           return;        /* found what we came for */
  7040.  
  7041.         if (kt->type != T_ENDIF) {
  7042.           if (if_stack->type == T_ELSE)
  7043.         error ("`#else' or `#elif' after `#else'");
  7044.           if_stack->type = kt->type;
  7045.           break;
  7046.         }
  7047.  
  7048.         temp = if_stack;
  7049.         if_stack = if_stack->next;
  7050.         free (temp);
  7051.         break;
  7052.       }
  7053.       break;
  7054.     }
  7055.       }
  7056.       /* Don't let erroneous code go by.  */
  7057.       if (kt->length < 0 && !lang_asm && pedantic)
  7058.     pedwarn ("invalid preprocessor directive name");
  7059.     }
  7060.   }
  7061.   ip->bufp = bp;
  7062.   /* after this returns, rescan will exit because ip->bufp
  7063.      now points to the end of the buffer.
  7064.      rescan is responsible for the error message also.  */
  7065. }
  7066.  
  7067. /*
  7068.  * handle a #else directive.  Do this by just continuing processing
  7069.  * without changing  if_stack ;  this is so that the error message
  7070.  * for missing #endif's etc. will point to the original #if.  It
  7071.  * is possible that something different would be better.
  7072.  */
  7073.  
  7074. static int
  7075. do_else (buf, limit, op, keyword)
  7076.      U_CHAR *buf, *limit;
  7077.      FILE_BUF *op;
  7078.      struct directive *keyword;
  7079. {
  7080.   FILE_BUF *ip = &instack[indepth];
  7081.  
  7082.   if (pedantic) {
  7083.     SKIP_WHITE_SPACE (buf);
  7084.     if (buf != limit)
  7085.       pedwarn ("text following `#else' violates ANSI standard");
  7086.   }
  7087.  
  7088.   if (if_stack == instack[indepth].if_stack) {
  7089.     error ("`#else' not within a conditional");
  7090.     return 0;
  7091.   } else {
  7092.     /* #ifndef can't have its special treatment for containing the whole file
  7093.        if it has a #else clause.  */
  7094.     if_stack->control_macro = 0;
  7095.  
  7096.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7097.       error ("`#else' after `#else'");
  7098.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7099.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7100.     fprintf (stderr, ", file %s", if_stack->fname);
  7101.       fprintf (stderr, ")\n");
  7102.     }
  7103.     if_stack->type = T_ELSE;
  7104.   }
  7105.  
  7106.   if (if_stack->if_succeeded)
  7107.     skip_if_group (ip, 0);
  7108.   else {
  7109.     ++if_stack->if_succeeded;    /* continue processing input */
  7110.     output_line_command (ip, op, 1, same_file);
  7111.   }
  7112.   return 0;
  7113. }
  7114.  
  7115. /*
  7116.  * unstack after #endif command
  7117.  */
  7118.  
  7119. static int
  7120. do_endif (buf, limit, op, keyword)
  7121.      U_CHAR *buf, *limit;
  7122.      FILE_BUF *op;
  7123.      struct directive *keyword;
  7124. {
  7125.   if (pedantic) {
  7126.     SKIP_WHITE_SPACE (buf);
  7127.     if (buf != limit)
  7128.       pedwarn ("text following `#endif' violates ANSI standard");
  7129.   }
  7130.  
  7131.   if (if_stack == instack[indepth].if_stack)
  7132.     error ("unbalanced `#endif'");
  7133.   else {
  7134.     IF_STACK_FRAME *temp = if_stack;
  7135.     if_stack = if_stack->next;
  7136.     if (temp->control_macro != 0) {
  7137.       /* This #endif matched a #ifndef at the start of the file.
  7138.      See if it is at the end of the file.  */
  7139.       FILE_BUF *ip = &instack[indepth];
  7140.       U_CHAR *p = ip->bufp;
  7141.       U_CHAR *ep = ip->buf + ip->length;
  7142.  
  7143.       while (p != ep) {
  7144.     U_CHAR c = *p++;
  7145.     switch (c) {
  7146.     case ' ':
  7147.     case '\t':
  7148.     case '\n':
  7149.       break;
  7150.     case '/':
  7151.       if (p != ep && *p == '*') {
  7152.         /* Skip this comment.  */
  7153.         int junk = 0;
  7154.         U_CHAR *save_bufp = ip->bufp;
  7155.         ip->bufp = p + 1;
  7156.         p = skip_to_end_of_comment (ip, &junk, 1);
  7157.         ip->bufp = save_bufp;
  7158.       }
  7159.       break;
  7160.     default:
  7161.       goto fail;
  7162.     }
  7163.       }
  7164.       /* If we get here, this #endif ends a #ifndef
  7165.      that contains all of the file (aside from whitespace).
  7166.      Arrange not to include the file again
  7167.      if the macro that was tested is defined.
  7168.  
  7169.      Do not do this for the top-level file in a -include or any
  7170.      file in a -imacros.  */
  7171.       if (indepth != 0
  7172.       && ! (indepth == 1 && no_record_file)
  7173.       && ! (no_record_file && no_output))
  7174.     record_control_macro (ip->fname, temp->control_macro);
  7175.     fail: ;
  7176.     }
  7177.     free (temp);
  7178.     output_line_command (&instack[indepth], op, 1, same_file);
  7179.   }
  7180.   return 0;
  7181. }
  7182.  
  7183. /* When an #else or #endif is found while skipping failed conditional,
  7184.    if -pedantic was specified, this is called to warn about text after
  7185.    the command name.  P points to the first char after the command name.  */
  7186.  
  7187. static void
  7188. validate_else (p)
  7189.      register U_CHAR *p;
  7190. {
  7191.   /* Advance P over whitespace and comments.  */
  7192.   while (1) {
  7193.     if (*p == '\\' && p[1] == '\n')
  7194.       p += 2;
  7195.     if (is_hor_space[*p])
  7196.       p++;
  7197.     else if (*p == '/') {
  7198.       if (p[1] == '\\' && p[2] == '\n')
  7199.     newline_fix (p + 1);
  7200.       if (p[1] == '*') {
  7201.     p += 2;
  7202.     /* Don't bother warning about unterminated comments
  7203.        since that will happen later.  Just be sure to exit.  */
  7204.     while (*p) {
  7205.       if (p[1] == '\\' && p[2] == '\n')
  7206.         newline_fix (p + 1);
  7207.       if (*p == '*' && p[1] == '/') {
  7208.         p += 2;
  7209.         break;
  7210.       }
  7211.       p++;
  7212.     }
  7213.       }
  7214.       else if (cplusplus_comments && p[1] == '/') {
  7215.     p += 2;
  7216.     while (*p && (*p != '\n' || p[-1] == '\\'))
  7217.       p++;
  7218.       }
  7219.     } else break;
  7220.   }
  7221.   if (*p && *p != '\n')
  7222.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  7223. }
  7224.  
  7225. /* Skip a comment, assuming the input ptr immediately follows the
  7226.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  7227.    (The canonical line counter is &ip->lineno.)
  7228.    Don't use this routine (or the next one) if bumping the line
  7229.    counter is not sufficient to deal with newlines in the string.
  7230.  
  7231.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  7232.    This feature is useful when processing a comment that is going to be
  7233.    processed or was processed at another point in the preprocessor,
  7234.    to avoid a duplicate warning.  Likewise for unterminated comment errors.  */
  7235.  
  7236. static U_CHAR *
  7237. skip_to_end_of_comment (ip, line_counter, nowarn)
  7238.      register FILE_BUF *ip;
  7239.      int *line_counter;        /* place to remember newlines, or NULL */
  7240.      int nowarn;
  7241. {
  7242.   register U_CHAR *limit = ip->buf + ip->length;
  7243.   register U_CHAR *bp = ip->bufp;
  7244.   FILE_BUF *op = &outbuf;    /* JF */
  7245.   int output = put_out_comments && !line_counter;
  7246.   int start_line = line_counter ? *line_counter : 0;
  7247.  
  7248.     /* JF this line_counter stuff is a crock to make sure the
  7249.        comment is only put out once, no matter how many times
  7250.        the comment is skipped.  It almost works */
  7251.   if (output) {
  7252.     *op->bufp++ = '/';
  7253.     *op->bufp++ = '*';
  7254.   }
  7255.   if (cplusplus_comments && bp[-1] == '/') {
  7256.     if (output) {
  7257.       while (bp < limit) {
  7258.     *op->bufp++ = *bp;
  7259.     if (*bp == '\n' && bp[-1] != '\\')
  7260.       break;
  7261.     if (*bp == '\n') {
  7262.       ++*line_counter;
  7263.       ++op->lineno;
  7264.     }
  7265.     bp++;
  7266.       }
  7267.       op->bufp[-1] = '*';
  7268.       *op->bufp++ = '/';
  7269.       *op->bufp++ = '\n';
  7270.     } else {
  7271.       while (bp < limit) {
  7272.     if (bp[-1] != '\\' && *bp == '\n') {
  7273.       break;
  7274.     } else {
  7275.       if (*bp == '\n' && line_counter)
  7276.         ++*line_counter;
  7277.       bp++;
  7278.     }
  7279.       }
  7280.     }
  7281.     ip->bufp = bp;
  7282.     return bp;
  7283.   }
  7284.   while (bp < limit) {
  7285.     if (output)
  7286.       *op->bufp++ = *bp;
  7287.     switch (*bp++) {
  7288.     case '/':
  7289.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  7290.     warning ("`/*' within comment");
  7291.       break;
  7292.     case '\n':
  7293.       /* If this is the end of the file, we have an unterminated comment.
  7294.      Don't swallow the newline.  We are guaranteed that there will be a
  7295.      trailing newline and various pieces assume it's there.  */
  7296.       if (bp == limit)
  7297.     {
  7298.       --bp;
  7299.       --limit;
  7300.       break;
  7301.     }
  7302.       if (line_counter != NULL)
  7303.     ++*line_counter;
  7304.       if (output)
  7305.     ++op->lineno;
  7306.       break;
  7307.     case '*':
  7308.       if (*bp == '\\' && bp[1] == '\n')
  7309.     newline_fix (bp);
  7310.       if (*bp == '/') {
  7311.         if (output)
  7312.       *op->bufp++ = '/';
  7313.     ip->bufp = ++bp;
  7314.     return bp;
  7315.       }
  7316.       break;
  7317.     }
  7318.   }
  7319.  
  7320.   if (!nowarn)
  7321.     error_with_line (line_for_error (start_line), "unterminated comment");
  7322.   ip->bufp = bp;
  7323.   return bp;
  7324. }
  7325.  
  7326. /*
  7327.  * Skip over a quoted string.  BP points to the opening quote.
  7328.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  7329.  * START_LINE is the line number of the starting point (but it need
  7330.  * not be valid if the starting point is inside a macro expansion).
  7331.  *
  7332.  * The input stack state is not changed.
  7333.  *
  7334.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  7335.  * for each newline passed.
  7336.  *
  7337.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  7338.  * if we pass a backslash-newline.
  7339.  *
  7340.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  7341.  */
  7342. static U_CHAR *
  7343. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  7344.      register U_CHAR *bp;
  7345.      register U_CHAR *limit;
  7346.      int start_line;
  7347.      int *count_newlines;
  7348.      int *backslash_newlines_p;
  7349.      int *eofp;
  7350. {
  7351.   register U_CHAR c, match;
  7352.  
  7353.   match = *bp++;
  7354.   while (1) {
  7355.     if (bp >= limit) {
  7356.       error_with_line (line_for_error (start_line),
  7357.                "unterminated string or character constant");
  7358.       error_with_line (multiline_string_line,
  7359.                "possible real start of unterminated constant");
  7360.       multiline_string_line = 0;
  7361.       if (eofp)
  7362.     *eofp = 1;
  7363.       break;
  7364.     }
  7365.     c = *bp++;
  7366.     if (c == '\\') {
  7367.       while (*bp == '\\' && bp[1] == '\n') {
  7368.     if (backslash_newlines_p)
  7369.       *backslash_newlines_p = 1;
  7370.     if (count_newlines)
  7371.       ++*count_newlines;
  7372.     bp += 2;
  7373.       }
  7374.       if (*bp == '\n' && count_newlines) {
  7375.     if (backslash_newlines_p)
  7376.       *backslash_newlines_p = 1;
  7377.     ++*count_newlines;
  7378.       }
  7379.       bp++;
  7380.     } else if (c == '\n') {
  7381.       if (traditional) {
  7382.      /* Unterminated strings and character constants are 'legal'.  */
  7383.      bp--;    /* Don't consume the newline. */
  7384.      if (eofp)
  7385.        *eofp = 1;
  7386.      break;
  7387.       }
  7388.       if (pedantic || match == '\'') {
  7389.     error_with_line (line_for_error (start_line),
  7390.              "unterminated string or character constant");
  7391.     bp--;
  7392.     if (eofp)
  7393.       *eofp = 1;
  7394.     break;
  7395.       }
  7396.       /* If not traditional, then allow newlines inside strings.  */
  7397.       if (count_newlines)
  7398.     ++*count_newlines;
  7399.       if (multiline_string_line == 0)
  7400.     multiline_string_line = start_line;
  7401.     } else if (c == match)
  7402.       break;
  7403.   }
  7404.   return bp;
  7405. }
  7406.  
  7407. /* Place into DST a quoted string representing the string SRC.
  7408.    Return the address of DST's terminating null.  */
  7409. static char *
  7410. quote_string (dst, src)
  7411.      char *dst, *src;
  7412. {
  7413.   U_CHAR c;
  7414.  
  7415.   *dst++ = '\"';
  7416.   for (;;)
  7417.     switch ((c = *src++))
  7418.       {
  7419.       default:
  7420.         if (isprint (c))
  7421.       *dst++ = c;
  7422.     else
  7423.       {
  7424.         sprintf (dst, "\\%03o", c);
  7425.         dst += 4;
  7426.       }
  7427.     break;
  7428.  
  7429.       case '\"':
  7430.       case '\\':
  7431.     *dst++ = '\\';
  7432.     *dst++ = c;
  7433.     break;
  7434.       
  7435.       case '\0':
  7436.     *dst++ = '\"';
  7437.     *dst = '\0';
  7438.     return dst;
  7439.       }
  7440. }
  7441.  
  7442. /* Skip across a group of balanced parens, starting from IP->bufp.
  7443.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  7444.  
  7445.    This does not handle newlines, because it's used for the arg of #if,
  7446.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  7447.  
  7448. static U_CHAR *
  7449. skip_paren_group (ip)
  7450.      register FILE_BUF *ip;
  7451. {
  7452.   U_CHAR *limit = ip->buf + ip->length;
  7453.   U_CHAR *p = ip->bufp;
  7454.   int depth = 0;
  7455.   int lines_dummy = 0;
  7456.  
  7457.   while (p != limit) {
  7458.     int c = *p++;
  7459.     switch (c) {
  7460.     case '(':
  7461.       depth++;
  7462.       break;
  7463.  
  7464.     case ')':
  7465.       depth--;
  7466.       if (depth == 0)
  7467.     return ip->bufp = p;
  7468.       break;
  7469.  
  7470.     case '/':
  7471.       if (*p == '*') {
  7472.     ip->bufp = p;
  7473.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  7474.     p = ip->bufp;
  7475.       }
  7476.  
  7477.     case '"':
  7478.     case '\'':
  7479.       {
  7480.     int eofp = 0;
  7481.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  7482.     if (eofp)
  7483.       return ip->bufp = p;
  7484.       }
  7485.       break;
  7486.     }
  7487.   }
  7488.  
  7489.   ip->bufp = p;
  7490.   return p;
  7491. }
  7492.  
  7493. /*
  7494.  * write out a #line command, for instance, after an #include file.
  7495.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  7496.  * appear to be a no-op, and we can output a few newlines instead
  7497.  * if we want to increase the line number by a small amount.
  7498.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  7499.  */
  7500.  
  7501. static void
  7502. output_line_command (ip, op, conditional, file_change)
  7503.      FILE_BUF *ip, *op;
  7504.      int conditional;
  7505.      enum file_change_code file_change;
  7506. {
  7507.   int len;
  7508.   char *line_cmd_buf, *line_end;
  7509.  
  7510.   if (no_line_commands
  7511.       || ip->fname == NULL
  7512.       || no_output) {
  7513.     op->lineno = ip->lineno;
  7514.     return;
  7515.   }
  7516.  
  7517.   if (conditional) {
  7518.     if (ip->lineno == op->lineno)
  7519.       return;
  7520.  
  7521.     /* If the inherited line number is a little too small,
  7522.        output some newlines instead of a #line command.  */
  7523.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  7524.       check_expand (op, 10);
  7525.       while (ip->lineno > op->lineno) {
  7526.     *op->bufp++ = '\n';
  7527.     op->lineno++;
  7528.       }
  7529.       return;
  7530.     }
  7531.   }
  7532.  
  7533.   /* Don't output a line number of 0 if we can help it.  */
  7534.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  7535.       && *ip->bufp == '\n') {
  7536.     ip->lineno++;
  7537.     ip->bufp++;
  7538.   }
  7539.  
  7540.   line_cmd_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  7541. #ifdef OUTPUT_LINE_COMMANDS
  7542.   sprintf (line_cmd_buf, "#line %d ", ip->lineno);
  7543. #else
  7544.   sprintf (line_cmd_buf, "# %d ", ip->lineno);
  7545. #endif
  7546.   line_end = quote_string (line_cmd_buf + strlen (line_cmd_buf),
  7547.                ip->nominal_fname);
  7548.   if (file_change != same_file) {
  7549.     *line_end++ = ' ';
  7550.     *line_end++ = file_change == enter_file ? '1' : '2';
  7551.   }
  7552.   /* Tell cc1 if following text comes from a system header file.  */
  7553.   if (ip->system_header_p) {
  7554.     *line_end++ = ' ';
  7555.     *line_end++ = '3';
  7556.   }
  7557. #ifndef NO_IMPLICIT_EXTERN_C
  7558.   /* Tell cc1plus if following text should be treated as C.  */
  7559.   if (ip->system_header_p == 2 && cplusplus) {
  7560.     *line_end++ = ' ';
  7561.     *line_end++ = '4';
  7562.   }
  7563. #endif
  7564.   *line_end++ = '\n';
  7565.   len = line_end - line_cmd_buf;
  7566.   check_expand (op, len + 1);
  7567.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7568.     *op->bufp++ = '\n';
  7569.   bcopy ((char *) line_cmd_buf, (char *) op->bufp, len);
  7570.   op->bufp += len;
  7571.   op->lineno = ip->lineno;
  7572. }
  7573.  
  7574. /* This structure represents one parsed argument in a macro call.
  7575.    `raw' points to the argument text as written (`raw_length' is its length).
  7576.    `expanded' points to the argument's macro-expansion
  7577.    (its length is `expand_length').
  7578.    `stringified_length' is the length the argument would have
  7579.    if stringified.
  7580.    `use_count' is the number of times this macro arg is substituted
  7581.    into the macro.  If the actual use count exceeds 10, 
  7582.    the value stored is 10.
  7583.    `free1' and `free2', if nonzero, point to blocks to be freed
  7584.    when the macro argument data is no longer needed.  */
  7585.  
  7586. struct argdata {
  7587.   U_CHAR *raw, *expanded;
  7588.   int raw_length, expand_length;
  7589.   int stringified_length;
  7590.   U_CHAR *free1, *free2;
  7591.   char newlines;
  7592.   char comments;
  7593.   char use_count;
  7594. };
  7595.  
  7596. /* Expand a macro call.
  7597.    HP points to the symbol that is the macro being called.
  7598.    Put the result of expansion onto the input stack
  7599.    so that subsequent input by our caller will use it.
  7600.  
  7601.    If macro wants arguments, caller has already verified that
  7602.    an argument list follows; arguments come from the input stack.  */
  7603.  
  7604. static void
  7605. macroexpand (hp, op)
  7606.      HASHNODE *hp;
  7607.      FILE_BUF *op;
  7608. {
  7609.   int nargs;
  7610.   DEFINITION *defn = hp->value.defn;
  7611.   register U_CHAR *xbuf;
  7612.   int xbuf_len;
  7613.   int start_line = instack[indepth].lineno;
  7614.   int rest_args, rest_zero;
  7615.  
  7616.   CHECK_DEPTH (return;);
  7617.  
  7618.   /* it might not actually be a macro.  */
  7619.   if (hp->type != T_MACRO) {
  7620.     special_symbol (hp, op);
  7621.     return;
  7622.   }
  7623.  
  7624.   /* This macro is being used inside a #if, which means it must be */
  7625.   /* recorded as a precondition.  */
  7626.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  7627.     dump_single_macro (hp, pcp_outfile);
  7628.   
  7629.   nargs = defn->nargs;
  7630.  
  7631.   if (nargs >= 0) {
  7632.     register int i;
  7633.     struct argdata *args;
  7634.     char *parse_error = 0;
  7635.  
  7636.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  7637.  
  7638.     for (i = 0; i < nargs; i++) {
  7639.       args[i].raw = (U_CHAR *) "";
  7640.       args[i].expanded = 0;
  7641.       args[i].raw_length = args[i].expand_length
  7642.     = args[i].stringified_length = 0;
  7643.       args[i].free1 = args[i].free2 = 0;
  7644.       args[i].use_count = 0;
  7645.     }
  7646.  
  7647.     /* Parse all the macro args that are supplied.  I counts them.
  7648.        The first NARGS args are stored in ARGS.
  7649.        The rest are discarded.
  7650.        If rest_args is set then we assume macarg absorbed the rest of the args.
  7651.        */
  7652.     i = 0;
  7653.     rest_args = 0;
  7654.     do {
  7655.       /* Discard the open-parenthesis or comma before the next arg.  */
  7656.       ++instack[indepth].bufp;
  7657.       if (rest_args)
  7658.     continue;
  7659.       if (i < nargs || (nargs == 0 && i == 0)) {
  7660.     /* if we are working on last arg which absorbs rest of args... */
  7661.     if (i == nargs - 1 && defn->rest_args)
  7662.       rest_args = 1;
  7663.     parse_error = macarg (&args[i], rest_args);
  7664.       }
  7665.       else
  7666.     parse_error = macarg (NULL_PTR, 0);
  7667.       if (parse_error) {
  7668.     error_with_line (line_for_error (start_line), parse_error);
  7669.     break;
  7670.       }
  7671.       i++;
  7672.     } while (*instack[indepth].bufp != ')');
  7673.  
  7674.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  7675.     if (i == 1) {
  7676.       register U_CHAR *bp = args[0].raw;
  7677.       register U_CHAR *lim = bp + args[0].raw_length;
  7678.       /* cpp.texi says for foo ( ) we provide one argument.
  7679.      However, if foo wants just 0 arguments, treat this as 0.  */
  7680.       if (nargs == 0)
  7681.     while (bp != lim && is_space[*bp]) bp++;
  7682.       if (bp == lim)
  7683.     i = 0;
  7684.     }
  7685.  
  7686.     /* Don't output an error message if we have already output one for
  7687.        a parse error above.  */
  7688.     rest_zero = 0;
  7689.     if (nargs == 0 && i > 0) {
  7690.       if (! parse_error)
  7691.     error ("arguments given to macro `%s'", hp->name);
  7692.     } else if (i < nargs) {
  7693.       /* traditional C allows foo() if foo wants one argument.  */
  7694.       if (nargs == 1 && i == 0 && traditional)
  7695.     ;
  7696.       /* the rest args token is allowed to absorb 0 tokens */
  7697.       else if (i == nargs - 1 && defn->rest_args)
  7698.     rest_zero = 1;
  7699.       else if (parse_error)
  7700.     ;
  7701.       else if (i == 0)
  7702.     error ("macro `%s' used without args", hp->name);
  7703.       else if (i == 1)
  7704.     error ("macro `%s' used with just one arg", hp->name);
  7705.       else
  7706.     error ("macro `%s' used with only %d args", hp->name, i);
  7707.     } else if (i > nargs) {
  7708.       if (! parse_error)
  7709.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  7710.     }
  7711.  
  7712.     /* Swallow the closeparen.  */
  7713.     ++instack[indepth].bufp;
  7714.  
  7715.     /* If macro wants zero args, we parsed the arglist for checking only.
  7716.        Read directly from the macro definition.  */
  7717.     if (nargs == 0) {
  7718.       xbuf = defn->expansion;
  7719.       xbuf_len = defn->length;
  7720.     } else {
  7721.       register U_CHAR *exp = defn->expansion;
  7722.       register int offset;    /* offset in expansion,
  7723.                    copied a piece at a time */
  7724.       register int totlen;    /* total amount of exp buffer filled so far */
  7725.  
  7726.       register struct reflist *ap, *last_ap;
  7727.  
  7728.       /* Macro really takes args.  Compute the expansion of this call.  */
  7729.  
  7730.       /* Compute length in characters of the macro's expansion.
  7731.      Also count number of times each arg is used.  */
  7732.       xbuf_len = defn->length;
  7733.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  7734.     if (ap->stringify)
  7735.       xbuf_len += args[ap->argno].stringified_length;
  7736.     else if (ap->raw_before || ap->raw_after || traditional)
  7737.       /* Add 4 for two newline-space markers to prevent
  7738.          token concatenation.  */
  7739.       xbuf_len += args[ap->argno].raw_length + 4;
  7740.     else {
  7741.       /* We have an ordinary (expanded) occurrence of the arg.
  7742.          So compute its expansion, if we have not already.  */
  7743.       if (args[ap->argno].expanded == 0) {
  7744.         FILE_BUF obuf;
  7745.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  7746.                       args[ap->argno].raw + args[ap->argno].raw_length,
  7747.                       1, 0);
  7748.  
  7749.         args[ap->argno].expanded = obuf.buf;
  7750.         args[ap->argno].expand_length = obuf.length;
  7751.         args[ap->argno].free2 = obuf.buf;
  7752.       }
  7753.  
  7754.       /* Add 4 for two newline-space markers to prevent
  7755.          token concatenation.  */
  7756.       xbuf_len += args[ap->argno].expand_length + 4;
  7757.     }
  7758.     if (args[ap->argno].use_count < 10)
  7759.       args[ap->argno].use_count++;
  7760.       }
  7761.  
  7762.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  7763.  
  7764.       /* Generate in XBUF the complete expansion
  7765.      with arguments substituted in.
  7766.      TOTLEN is the total size generated so far.
  7767.      OFFSET is the index in the definition
  7768.      of where we are copying from.  */
  7769.       offset = totlen = 0;
  7770.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  7771.        last_ap = ap, ap = ap->next) {
  7772.     register struct argdata *arg = &args[ap->argno];
  7773.     int count_before = totlen;
  7774.  
  7775.     /* Add chars to XBUF.  */
  7776.     for (i = 0; i < ap->nchars; i++, offset++)
  7777.       xbuf[totlen++] = exp[offset];
  7778.  
  7779.     /* If followed by an empty rest arg with concatenation,
  7780.        delete the last run of nonwhite chars.  */
  7781.     if (rest_zero && totlen > count_before
  7782.         && ((ap->rest_args && ap->raw_before)
  7783.         || (last_ap != NULL && last_ap->rest_args
  7784.             && last_ap->raw_after))) {
  7785.       /* Delete final whitespace.  */
  7786.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  7787.         totlen--;
  7788.       }
  7789.  
  7790.       /* Delete the nonwhites before them.  */
  7791.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  7792.         totlen--;
  7793.       }
  7794.     }
  7795.  
  7796.     if (ap->stringify != 0) {
  7797.       int arglen = arg->raw_length;
  7798.       int escaped = 0;
  7799.       int in_string = 0;
  7800.       int c;
  7801.       i = 0;
  7802.       while (i < arglen
  7803.          && (c = arg->raw[i], is_space[c]))
  7804.         i++;
  7805.       while (i < arglen
  7806.          && (c = arg->raw[arglen - 1], is_space[c]))
  7807.         arglen--;
  7808.       if (!traditional)
  7809.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  7810.       for (; i < arglen; i++) {
  7811.         c = arg->raw[i];
  7812.  
  7813.         /* Special markers Newline Space
  7814.            generate nothing for a stringified argument.  */
  7815.         if (c == '\n' && arg->raw[i+1] != '\n') {
  7816.           i++;
  7817.           continue;
  7818.         }
  7819.  
  7820.         /* Internal sequences of whitespace are replaced by one space
  7821.            except within an string or char token.  */
  7822.         if (! in_string
  7823.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  7824.           while (1) {
  7825.         /* Note that Newline Space does occur within whitespace
  7826.            sequences; consider it part of the sequence.  */
  7827.         if (c == '\n' && is_space[arg->raw[i+1]])
  7828.           i += 2;
  7829.         else if (c != '\n' && is_space[c])
  7830.           i++;
  7831.         else break;
  7832.         c = arg->raw[i];
  7833.           }
  7834.           i--;
  7835.           c = ' ';
  7836.         }
  7837.  
  7838.         if (escaped)
  7839.           escaped = 0;
  7840.         else {
  7841.           if (c == '\\')
  7842.         escaped = 1;
  7843.           if (in_string) {
  7844.         if (c == in_string)
  7845.           in_string = 0;
  7846.           } else if (c == '\"' || c == '\'')
  7847.         in_string = c;
  7848.         }
  7849.  
  7850.         /* Escape these chars */
  7851.         if (c == '\"' || (in_string && c == '\\'))
  7852.           xbuf[totlen++] = '\\';
  7853.         if (isprint (c))
  7854.           xbuf[totlen++] = c;
  7855.         else {
  7856.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  7857.           totlen += 4;
  7858.         }
  7859.       }
  7860.       if (!traditional)
  7861.         xbuf[totlen++] = '\"'; /* insert ending quote */
  7862.     } else if (ap->raw_before || ap->raw_after || traditional) {
  7863.       U_CHAR *p1 = arg->raw;
  7864.       U_CHAR *l1 = p1 + arg->raw_length;
  7865.       if (ap->raw_before) {
  7866.         while (p1 != l1 && is_space[*p1]) p1++;
  7867.         while (p1 != l1 && is_idchar[*p1])
  7868.           xbuf[totlen++] = *p1++;
  7869.         /* Delete any no-reexpansion marker that follows
  7870.            an identifier at the beginning of the argument
  7871.            if the argument is concatenated with what precedes it.  */
  7872.         if (p1[0] == '\n' && p1[1] == '-')
  7873.           p1 += 2;
  7874.       } else if (!traditional) {
  7875.       /* Ordinary expanded use of the argument.
  7876.          Put in newline-space markers to prevent token pasting.  */
  7877.         xbuf[totlen++] = '\n';
  7878.         xbuf[totlen++] = ' ';
  7879.       }
  7880.       if (ap->raw_after) {
  7881.         /* Arg is concatenated after: delete trailing whitespace,
  7882.            whitespace markers, and no-reexpansion markers.  */
  7883.         while (p1 != l1) {
  7884.           if (is_space[l1[-1]]) l1--;
  7885.           else if (l1[-1] == '-') {
  7886.         U_CHAR *p2 = l1 - 1;
  7887.         /* If a `-' is preceded by an odd number of newlines then it
  7888.            and the last newline are a no-reexpansion marker.  */
  7889.         while (p2 != p1 && p2[-1] == '\n') p2--;
  7890.         if ((l1 - 1 - p2) & 1) {
  7891.           l1 -= 2;
  7892.         }
  7893.         else break;
  7894.           }
  7895.           else break;
  7896.         }
  7897.       }
  7898.  
  7899.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  7900.       totlen += l1 - p1;
  7901.       if (!traditional && !ap->raw_after) {
  7902.         /* Ordinary expanded use of the argument.
  7903.            Put in newline-space markers to prevent token pasting.  */
  7904.         xbuf[totlen++] = '\n';
  7905.         xbuf[totlen++] = ' ';
  7906.       }
  7907.     } else {
  7908.       /* Ordinary expanded use of the argument.
  7909.          Put in newline-space markers to prevent token pasting.  */
  7910.       if (!traditional) {
  7911.         xbuf[totlen++] = '\n';
  7912.         xbuf[totlen++] = ' ';
  7913.       }
  7914.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  7915.          arg->expand_length);
  7916.       totlen += arg->expand_length;
  7917.       if (!traditional) {
  7918.         xbuf[totlen++] = '\n';
  7919.         xbuf[totlen++] = ' ';
  7920.       }
  7921.       /* If a macro argument with newlines is used multiple times,
  7922.          then only expand the newlines once.  This avoids creating output
  7923.          lines which don't correspond to any input line, which confuses
  7924.          gdb and gcov.  */
  7925.       if (arg->use_count > 1 && arg->newlines > 0) {
  7926.         /* Don't bother doing change_newlines for subsequent
  7927.            uses of arg.  */
  7928.         arg->use_count = 1;
  7929.         arg->expand_length
  7930.           = change_newlines (arg->expanded, arg->expand_length);
  7931.       }
  7932.     }
  7933.  
  7934.     if (totlen > xbuf_len)
  7935.       abort ();
  7936.       }
  7937.  
  7938.       /* if there is anything left of the definition
  7939.      after handling the arg list, copy that in too. */
  7940.  
  7941.       for (i = offset; i < defn->length; i++) {
  7942.     /* if we've reached the end of the macro */
  7943.     if (exp[i] == ')')
  7944.       rest_zero = 0;
  7945.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  7946.            && last_ap->raw_after))
  7947.       xbuf[totlen++] = exp[i];
  7948.       }
  7949.  
  7950.       xbuf[totlen] = 0;
  7951.       xbuf_len = totlen;
  7952.  
  7953.       for (i = 0; i < nargs; i++) {
  7954.     if (args[i].free1 != 0)
  7955.       free (args[i].free1);
  7956.     if (args[i].free2 != 0)
  7957.       free (args[i].free2);
  7958.       }
  7959.     }
  7960.   } else {
  7961.     xbuf = defn->expansion;
  7962.     xbuf_len = defn->length;
  7963.   }
  7964.  
  7965.   /* Now put the expansion on the input stack
  7966.      so our caller will commence reading from it.  */
  7967.   {
  7968.     register FILE_BUF *ip2;
  7969.  
  7970.     ip2 = &instack[++indepth];
  7971.  
  7972.     ip2->fname = 0;
  7973.     ip2->nominal_fname = 0;
  7974.     /* This may not be exactly correct, but will give much better error
  7975.        messages for nested macro calls than using a line number of zero.  */
  7976.     ip2->lineno = start_line;
  7977.     ip2->buf = xbuf;
  7978.     ip2->length = xbuf_len;
  7979.     ip2->bufp = xbuf;
  7980.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  7981.     ip2->macro = hp;
  7982.     ip2->if_stack = if_stack;
  7983.     ip2->system_header_p = 0;
  7984.  
  7985.     /* Recursive macro use sometimes works traditionally.
  7986.        #define foo(x,y) bar (x (y,0), y)
  7987.        foo (foo, baz)  */
  7988.  
  7989.     if (!traditional)
  7990.       hp->type = T_DISABLED;
  7991.   }
  7992. }
  7993.  
  7994. /*
  7995.  * Parse a macro argument and store the info on it into *ARGPTR.
  7996.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  7997.  * Return nonzero to indicate a syntax error.
  7998.  */
  7999.  
  8000. static char *
  8001. macarg (argptr, rest_args)
  8002.      register struct argdata *argptr;
  8003.      int rest_args;
  8004. {
  8005.   FILE_BUF *ip = &instack[indepth];
  8006.   int paren = 0;
  8007.   int newlines = 0;
  8008.   int comments = 0;
  8009.  
  8010.   /* Try to parse as much of the argument as exists at this
  8011.      input stack level.  */
  8012.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  8013.             &paren, &newlines, &comments, rest_args);
  8014.  
  8015.   /* If we find the end of the argument at this level,
  8016.      set up *ARGPTR to point at it in the input stack.  */
  8017.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  8018.       && bp != ip->buf + ip->length) {
  8019.     if (argptr != 0) {
  8020.       argptr->raw = ip->bufp;
  8021.       argptr->raw_length = bp - ip->bufp;
  8022.       argptr->newlines = newlines;
  8023.     }
  8024.     ip->bufp = bp;
  8025.   } else {
  8026.     /* This input stack level ends before the macro argument does.
  8027.        We must pop levels and keep parsing.
  8028.        Therefore, we must allocate a temporary buffer and copy
  8029.        the macro argument into it.  */
  8030.     int bufsize = bp - ip->bufp;
  8031.     int extra = newlines;
  8032.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  8033.     int final_start = 0;
  8034.  
  8035.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  8036.     ip->bufp = bp;
  8037.     ip->lineno += newlines;
  8038.  
  8039.     while (bp == ip->buf + ip->length) {
  8040.       if (instack[indepth].macro == 0) {
  8041.     free (buffer);
  8042.     return "unterminated macro call";
  8043.       }
  8044.       ip->macro->type = T_MACRO;
  8045.       if (ip->free_ptr)
  8046.     free (ip->free_ptr);
  8047.       ip = &instack[--indepth];
  8048.       newlines = 0;
  8049.       comments = 0;
  8050.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  8051.             &newlines, &comments, rest_args);
  8052.       final_start = bufsize;
  8053.       bufsize += bp - ip->bufp;
  8054.       extra += newlines;
  8055.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  8056.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  8057.          bp - ip->bufp);
  8058.       ip->bufp = bp;
  8059.       ip->lineno += newlines;
  8060.     }
  8061.  
  8062.     /* Now, if arg is actually wanted, record its raw form,
  8063.        discarding comments and duplicating newlines in whatever
  8064.        part of it did not come from a macro expansion.
  8065.        EXTRA space has been preallocated for duplicating the newlines.
  8066.        FINAL_START is the index of the start of that part.  */
  8067.     if (argptr != 0) {
  8068.       argptr->raw = buffer;
  8069.       argptr->raw_length = bufsize;
  8070.       argptr->free1 = buffer;
  8071.       argptr->newlines = newlines;
  8072.       argptr->comments = comments;
  8073.       if ((newlines || comments) && ip->fname != 0)
  8074.     argptr->raw_length
  8075.       = final_start +
  8076.         discard_comments (argptr->raw + final_start,
  8077.                   argptr->raw_length - final_start,
  8078.                   newlines);
  8079.       argptr->raw[argptr->raw_length] = 0;
  8080.       if (argptr->raw_length > bufsize + extra)
  8081.     abort ();
  8082.     }
  8083.   }
  8084.  
  8085.   /* If we are not discarding this argument,
  8086.      macroexpand it and compute its length as stringified.
  8087.      All this info goes into *ARGPTR.  */
  8088.  
  8089.   if (argptr != 0) {
  8090.     register U_CHAR *buf, *lim;
  8091.     register int totlen;
  8092.  
  8093.     buf = argptr->raw;
  8094.     lim = buf + argptr->raw_length;
  8095.  
  8096.     while (buf != lim && is_space[*buf])
  8097.       buf++;
  8098.     while (buf != lim && is_space[lim[-1]])
  8099.       lim--;
  8100.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  8101.     while (buf != lim) {
  8102.       register U_CHAR c = *buf++;
  8103.       totlen++;
  8104.       /* Internal sequences of whitespace are replaced by one space
  8105.      in most cases, but not always.  So count all the whitespace
  8106.      in case we need to keep it all.  */
  8107. #if 0
  8108.       if (is_space[c])
  8109.     SKIP_ALL_WHITE_SPACE (buf);
  8110.       else
  8111. #endif
  8112.       if (c == '\"' || c == '\\') /* escape these chars */
  8113.     totlen++;
  8114.       else if (!isprint (c))
  8115.     totlen += 3;
  8116.     }
  8117.     argptr->stringified_length = totlen;
  8118.   }
  8119.   return 0;
  8120. }
  8121.  
  8122. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  8123.    counting parens in *DEPTHPTR,
  8124.    and return if reach LIMIT
  8125.    or before a `)' that would make *DEPTHPTR negative
  8126.    or before a comma when *DEPTHPTR is zero.
  8127.    Single and double quotes are matched and termination
  8128.    is inhibited within them.  Comments also inhibit it.
  8129.    Value returned is pointer to stopping place.
  8130.  
  8131.    Increment *NEWLINES each time a newline is passed.
  8132.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  8133.    Set *COMMENTS to 1 if a comment is seen.  */
  8134.  
  8135. static U_CHAR *
  8136. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  8137.      U_CHAR *start;
  8138.      register U_CHAR *limit;
  8139.      int *depthptr, *newlines, *comments;
  8140.      int rest_args;
  8141. {
  8142.   register U_CHAR *bp = start;
  8143.  
  8144.   while (bp < limit) {
  8145.     switch (*bp) {
  8146.     case '(':
  8147.       (*depthptr)++;
  8148.       break;
  8149.     case ')':
  8150.       if (--(*depthptr) < 0)
  8151.     return bp;
  8152.       break;
  8153.     case '\\':
  8154.       /* Traditionally, backslash makes following char not special.  */
  8155.       if (bp + 1 < limit && traditional)
  8156.     {
  8157.       bp++;
  8158.       /* But count source lines anyway.  */
  8159.       if (*bp == '\n')
  8160.         ++*newlines;
  8161.     }
  8162.       break;
  8163.     case '\n':
  8164.       ++*newlines;
  8165.       break;
  8166.     case '/':
  8167.       if (bp[1] == '\\' && bp[2] == '\n')
  8168.     newline_fix (bp + 1);
  8169.       if (cplusplus_comments && bp[1] == '/') {
  8170.     *comments = 1;
  8171.     bp += 2;
  8172.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  8173.       if (*bp == '\n') ++*newlines;
  8174.       bp++;
  8175.     }
  8176.     break;
  8177.       }
  8178.       if (bp[1] != '*' || bp + 1 >= limit)
  8179.     break;
  8180.       *comments = 1;
  8181.       bp += 2;
  8182.       while (bp + 1 < limit) {
  8183.     if (bp[0] == '*'
  8184.         && bp[1] == '\\' && bp[2] == '\n')
  8185.       newline_fix (bp + 1);
  8186.     if (bp[0] == '*' && bp[1] == '/')
  8187.       break;
  8188.     if (*bp == '\n') ++*newlines;
  8189.     bp++;
  8190.       }
  8191.       break;
  8192.     case '\'':
  8193.     case '\"':
  8194.       {
  8195.     int quotec;
  8196.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  8197.       if (*bp == '\\') {
  8198.         bp++;
  8199.         if (*bp == '\n')
  8200.           ++*newlines;
  8201.         while (*bp == '\\' && bp[1] == '\n') {
  8202.           bp += 2;
  8203.         }
  8204.       } else if (*bp == '\n') {
  8205.         ++*newlines;
  8206.         if (quotec == '\'')
  8207.           break;
  8208.       }
  8209.     }
  8210.       }
  8211.       break;
  8212.     case ',':
  8213.       /* if we've returned to lowest level and we aren't absorbing all args */
  8214.       if ((*depthptr) == 0 && rest_args == 0)
  8215.     return bp;
  8216.       break;
  8217.     }
  8218.     bp++;
  8219.   }
  8220.  
  8221.   return bp;
  8222. }
  8223.  
  8224. /* Discard comments and duplicate newlines
  8225.    in the string of length LENGTH at START,
  8226.    except inside of string constants.
  8227.    The string is copied into itself with its beginning staying fixed.  
  8228.  
  8229.    NEWLINES is the number of newlines that must be duplicated.
  8230.    We assume that that much extra space is available past the end
  8231.    of the string.  */
  8232.  
  8233. static int
  8234. discard_comments (start, length, newlines)
  8235.      U_CHAR *start;
  8236.      int length;
  8237.      int newlines;
  8238. {
  8239.   register U_CHAR *ibp;
  8240.   register U_CHAR *obp;
  8241.   register U_CHAR *limit;
  8242.   register int c;
  8243.  
  8244.   /* If we have newlines to duplicate, copy everything
  8245.      that many characters up.  Then, in the second part,
  8246.      we will have room to insert the newlines
  8247.      while copying down.
  8248.      NEWLINES may actually be too large, because it counts
  8249.      newlines in string constants, and we don't duplicate those.
  8250.      But that does no harm.  */
  8251.   if (newlines > 0) {
  8252.     ibp = start + length;
  8253.     obp = ibp + newlines;
  8254.     limit = start;
  8255.     while (limit != ibp)
  8256.       *--obp = *--ibp;
  8257.   }
  8258.  
  8259.   ibp = start + newlines;
  8260.   limit = start + length + newlines;
  8261.   obp = start;
  8262.  
  8263.   while (ibp < limit) {
  8264.     *obp++ = c = *ibp++;
  8265.     switch (c) {
  8266.     case '\n':
  8267.       /* Duplicate the newline.  */
  8268.       *obp++ = '\n';
  8269.       break;
  8270.  
  8271.     case '\\':
  8272.       if (*ibp == '\n') {
  8273.     obp--;
  8274.     ibp++;
  8275.       }
  8276.       break;
  8277.  
  8278.     case '/':
  8279.       if (*ibp == '\\' && ibp[1] == '\n')
  8280.     newline_fix (ibp);
  8281.       /* Delete any comment.  */
  8282.       if (cplusplus_comments && ibp[0] == '/') {
  8283.     /* Comments are equivalent to spaces.  */
  8284.     obp[-1] = ' ';
  8285.     ibp++;
  8286.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  8287.       ibp++;
  8288.     break;
  8289.       }
  8290.       if (ibp[0] != '*' || ibp + 1 >= limit)
  8291.     break;
  8292.       /* Comments are equivalent to spaces.  */
  8293.       obp[-1] = ' ';
  8294.       ibp++;
  8295.       while (ibp + 1 < limit) {
  8296.     if (ibp[0] == '*'
  8297.         && ibp[1] == '\\' && ibp[2] == '\n')
  8298.       newline_fix (ibp + 1);
  8299.     if (ibp[0] == '*' && ibp[1] == '/')
  8300.       break;
  8301.     ibp++;
  8302.       }
  8303.       ibp += 2;
  8304.       break;
  8305.  
  8306.     case '\'':
  8307.     case '\"':
  8308.       /* Notice and skip strings, so that we don't
  8309.      think that comments start inside them,
  8310.      and so we don't duplicate newlines in them.  */
  8311.       {
  8312.     int quotec = c;
  8313.     while (ibp < limit) {
  8314.       *obp++ = c = *ibp++;
  8315.       if (c == quotec)
  8316.         break;
  8317.       if (c == '\n' && quotec == '\'')
  8318.         break;
  8319.       if (c == '\\' && ibp < limit) {
  8320.         while (*ibp == '\\' && ibp[1] == '\n')
  8321.           ibp += 2;
  8322.         *obp++ = *ibp++;
  8323.       }
  8324.     }
  8325.       }
  8326.       break;
  8327.     }
  8328.   }
  8329.  
  8330.   return obp - start;
  8331. }
  8332.  
  8333. /* Turn newlines to spaces in the string of length LENGTH at START,
  8334.    except inside of string constants.
  8335.    The string is copied into itself with its beginning staying fixed.  */
  8336.  
  8337. static int
  8338. change_newlines (start, length)
  8339.      U_CHAR *start;
  8340.      int length;
  8341. {
  8342.   register U_CHAR *ibp;
  8343.   register U_CHAR *obp;
  8344.   register U_CHAR *limit;
  8345.   register int c;
  8346.  
  8347.   ibp = start;
  8348.   limit = start + length;
  8349.   obp = start;
  8350.  
  8351.   while (ibp < limit) {
  8352.     *obp++ = c = *ibp++;
  8353.     switch (c) {
  8354.     case '\n':
  8355.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  8356.      string.  Skip past the newline and its duplicate.
  8357.      Put a space in the output.  */
  8358.       if (*ibp == '\n')
  8359.     {
  8360.       ibp++;
  8361.       obp--;
  8362.       *obp++ = ' ';
  8363.     }
  8364.       break;
  8365.  
  8366.     case '\'':
  8367.     case '\"':
  8368.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  8369.       {
  8370.     int quotec = c;
  8371.     while (ibp < limit) {
  8372.       *obp++ = c = *ibp++;
  8373.       if (c == quotec)
  8374.         break;
  8375.       if (c == '\n' && quotec == '\'')
  8376.         break;
  8377.     }
  8378.       }
  8379.       break;
  8380.     }
  8381.   }
  8382.  
  8383.   return obp - start;
  8384. }
  8385.  
  8386. /*
  8387.  * error - print error message and increment count of errors.
  8388.  */
  8389.  
  8390. void
  8391. error (msg, arg1, arg2, arg3)
  8392.      char *msg;
  8393.      char *arg1, *arg2, *arg3;
  8394. {
  8395.   int i;
  8396.   FILE_BUF *ip = NULL;
  8397.  
  8398.   print_containing_files ();
  8399.  
  8400.   for (i = indepth; i >= 0; i--)
  8401.     if (instack[i].fname != NULL) {
  8402.       ip = &instack[i];
  8403.       break;
  8404.     }
  8405.  
  8406.   if (ip != NULL)
  8407.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8408.   fprintf (stderr, msg, arg1, arg2, arg3);
  8409.   fprintf (stderr, "\n");
  8410.   errors++;
  8411. }
  8412.  
  8413. /* Error including a message from `errno'.  */
  8414.  
  8415. static void
  8416. error_from_errno (name)
  8417.      char *name;
  8418. {
  8419.   int i;
  8420.   FILE_BUF *ip = NULL;
  8421.  
  8422.   print_containing_files ();
  8423.  
  8424.   for (i = indepth; i >= 0; i--)
  8425.     if (instack[i].fname != NULL) {
  8426.       ip = &instack[i];
  8427.       break;
  8428.     }
  8429.  
  8430.   if (ip != NULL)
  8431.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8432.  
  8433.   if (errno < sys_nerr)
  8434.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  8435.   else
  8436.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  8437.  
  8438.   errors++;
  8439. }
  8440.  
  8441. /* Print error message but don't count it.  */
  8442.  
  8443. void
  8444. warning (msg, arg1, arg2, arg3)
  8445.      char *msg;
  8446.      char *arg1, *arg2, *arg3;
  8447. {
  8448.   int i;
  8449.   FILE_BUF *ip = NULL;
  8450.  
  8451.   if (inhibit_warnings)
  8452.     return;
  8453.  
  8454.   if (warnings_are_errors)
  8455.     errors++;
  8456.  
  8457.   print_containing_files ();
  8458.  
  8459.   for (i = indepth; i >= 0; i--)
  8460.     if (instack[i].fname != NULL) {
  8461.       ip = &instack[i];
  8462.       break;
  8463.     }
  8464.  
  8465.   if (ip != NULL)
  8466.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8467.   fprintf (stderr, "warning: ");
  8468.   fprintf (stderr, msg, arg1, arg2, arg3);
  8469.   fprintf (stderr, "\n");
  8470. }
  8471.  
  8472. static void
  8473. error_with_line (line, msg, arg1, arg2, arg3)
  8474.      int line;
  8475.      char *msg;
  8476.      char *arg1, *arg2, *arg3;
  8477. {
  8478.   int i;
  8479.   FILE_BUF *ip = NULL;
  8480.  
  8481.   print_containing_files ();
  8482.  
  8483.   for (i = indepth; i >= 0; i--)
  8484.     if (instack[i].fname != NULL) {
  8485.       ip = &instack[i];
  8486.       break;
  8487.     }
  8488.  
  8489.   if (ip != NULL)
  8490.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  8491.   fprintf (stderr, msg, arg1, arg2, arg3);
  8492.   fprintf (stderr, "\n");
  8493.   errors++;
  8494. }
  8495.  
  8496. static void
  8497. warning_with_line (line, msg, arg1, arg2, arg3)
  8498.      int line;
  8499.      char *msg;
  8500.      char *arg1, *arg2, *arg3;
  8501. {
  8502.   int i;
  8503.   FILE_BUF *ip = NULL;
  8504.  
  8505.   if (inhibit_warnings)
  8506.     return;
  8507.  
  8508.   if (warnings_are_errors)
  8509.     errors++;
  8510.  
  8511.   print_containing_files ();
  8512.  
  8513.   for (i = indepth; i >= 0; i--)
  8514.     if (instack[i].fname != NULL) {
  8515.       ip = &instack[i];
  8516.       break;
  8517.     }
  8518.  
  8519.   if (ip != NULL)
  8520.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  8521.   fprintf (stderr, "warning: ");
  8522.   fprintf (stderr, msg, arg1, arg2, arg3);
  8523.   fprintf (stderr, "\n");
  8524. }
  8525.  
  8526. /* print an error message and maybe count it.  */
  8527.  
  8528. void
  8529. pedwarn (msg, arg1, arg2, arg3)
  8530.      char *msg;
  8531.      char *arg1, *arg2, *arg3;
  8532. {
  8533.   if (pedantic_errors)
  8534.     error (msg, arg1, arg2, arg3);
  8535.   else
  8536.     warning (msg, arg1, arg2, arg3);
  8537. }
  8538.  
  8539. void
  8540. pedwarn_with_line (line, msg, arg1, arg2, arg3)
  8541.      int line;
  8542.      char *msg;
  8543.      char *arg1, *arg2, *arg3;
  8544. {
  8545.   if (pedantic_errors)
  8546.     error_with_line (line, msg, arg1, arg2, arg3);
  8547.   else
  8548.     warning_with_line (line, msg, arg1, arg2, arg3);
  8549. }
  8550.  
  8551. /* Report a warning (or an error if pedantic_errors)
  8552.    giving specified file name and line number, not current.  */
  8553.  
  8554. static void
  8555. pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
  8556.      char *file;
  8557.      int line;
  8558.      char *msg;
  8559.      char *arg1, *arg2, *arg3;
  8560. {
  8561.   if (!pedantic_errors && inhibit_warnings)
  8562.     return;
  8563.   if (file != NULL)
  8564.     fprintf (stderr, "%s:%d: ", file, line);
  8565.   if (pedantic_errors)
  8566.     errors++;
  8567.   if (!pedantic_errors)
  8568.     fprintf (stderr, "warning: ");
  8569.   fprintf (stderr, msg, arg1, arg2, arg3);
  8570.   fprintf (stderr, "\n");
  8571. }
  8572.  
  8573. /* Print the file names and line numbers of the #include
  8574.    commands which led to the current file.  */
  8575.  
  8576. static void
  8577. print_containing_files ()
  8578. {
  8579.   FILE_BUF *ip = NULL;
  8580.   int i;
  8581.   int first = 1;
  8582.  
  8583.   /* If stack of files hasn't changed since we last printed
  8584.      this info, don't repeat it.  */
  8585.   if (last_error_tick == input_file_stack_tick)
  8586.     return;
  8587.  
  8588.   for (i = indepth; i >= 0; i--)
  8589.     if (instack[i].fname != NULL) {
  8590.       ip = &instack[i];
  8591.       break;
  8592.     }
  8593.  
  8594.   /* Give up if we don't find a source file.  */
  8595.   if (ip == NULL)
  8596.     return;
  8597.  
  8598.   /* Find the other, outer source files.  */
  8599.   for (i--; i >= 0; i--)
  8600.     if (instack[i].fname != NULL) {
  8601.       ip = &instack[i];
  8602.       if (first) {
  8603.     first = 0;
  8604.     fprintf (stderr, "In file included");
  8605.       } else {
  8606.     fprintf (stderr, ",\n                ");
  8607.       }
  8608.  
  8609.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  8610.     }
  8611.   if (! first)
  8612.     fprintf (stderr, ":\n");
  8613.  
  8614.   /* Record we have printed the status as of this time.  */
  8615.   last_error_tick = input_file_stack_tick;
  8616. }
  8617.  
  8618. /* Return the line at which an error occurred.
  8619.    The error is not necessarily associated with the current spot
  8620.    in the input stack, so LINE says where.  LINE will have been
  8621.    copied from ip->lineno for the current input level.
  8622.    If the current level is for a file, we return LINE.
  8623.    But if the current level is not for a file, LINE is meaningless.
  8624.    In that case, we return the lineno of the innermost file.  */
  8625.  
  8626. static int
  8627. line_for_error (line)
  8628.      int line;
  8629. {
  8630.   int i;
  8631.   int line1 = line;
  8632.  
  8633.   for (i = indepth; i >= 0; ) {
  8634.     if (instack[i].fname != 0)
  8635.       return line1;
  8636.     i--;
  8637.     if (i < 0)
  8638.       return 0;
  8639.     line1 = instack[i].lineno;
  8640.   }
  8641.   abort ();
  8642.   /*NOTREACHED*/
  8643.   return 0;
  8644. }
  8645.  
  8646. /*
  8647.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  8648.  *
  8649.  * As things stand, nothing is ever placed in the output buffer to be
  8650.  * removed again except when it's KNOWN to be part of an identifier,
  8651.  * so flushing and moving down everything left, instead of expanding,
  8652.  * should work ok.
  8653.  */
  8654.  
  8655. /* You might think void was cleaner for the return type,
  8656.    but that would get type mismatch in check_expand in strict ANSI.  */
  8657. static int
  8658. grow_outbuf (obuf, needed)
  8659.      register FILE_BUF *obuf;
  8660.      register int needed;
  8661. {
  8662.   register U_CHAR *p;
  8663.   int minsize;
  8664.  
  8665.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  8666.     return 0;
  8667.  
  8668.   /* Make it at least twice as big as it is now.  */
  8669.   obuf->length *= 2;
  8670.   /* Make it have at least 150% of the free space we will need.  */
  8671.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  8672.   if (minsize > obuf->length)
  8673.     obuf->length = minsize;
  8674.  
  8675.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  8676.     memory_full ();
  8677.  
  8678.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  8679.   obuf->buf = p;
  8680.  
  8681.   return 0;
  8682. }
  8683.  
  8684. /* Symbol table for macro names and special symbols */
  8685.  
  8686. /*
  8687.  * install a name in the main hash table, even if it is already there.
  8688.  *   name stops with first non alphanumeric, except leading '#'.
  8689.  * caller must check against redefinition if that is desired.
  8690.  * delete_macro () removes things installed by install () in fifo order.
  8691.  * this is important because of the `defined' special symbol used
  8692.  * in #if, and also if pushdef/popdef directives are ever implemented.
  8693.  *
  8694.  * If LEN is >= 0, it is the length of the name.
  8695.  * Otherwise, compute the length by scanning the entire name.
  8696.  *
  8697.  * If HASH is >= 0, it is the precomputed hash code.
  8698.  * Otherwise, compute the hash code.
  8699.  */
  8700. static HASHNODE *
  8701. install (name, len, type, ivalue, value, hash)
  8702.      U_CHAR *name;
  8703.      int len;
  8704.      enum node_type type;
  8705.      int ivalue;
  8706.      char *value;
  8707.      int hash;
  8708. {
  8709.   register HASHNODE *hp;
  8710.   register int i, bucket;
  8711.   register U_CHAR *p, *q;
  8712.  
  8713.   if (len < 0) {
  8714.     p = name;
  8715.     while (is_idchar[*p])
  8716.       p++;
  8717.     len = p - name;
  8718.   }
  8719.  
  8720.   if (hash < 0)
  8721.     hash = hashf (name, len, HASHSIZE);
  8722.  
  8723.   i = sizeof (HASHNODE) + len + 1;
  8724.   hp = (HASHNODE *) xmalloc (i);
  8725.   bucket = hash;
  8726.   hp->bucket_hdr = &hashtab[bucket];
  8727.   hp->next = hashtab[bucket];
  8728.   hashtab[bucket] = hp;
  8729.   hp->prev = NULL;
  8730.   if (hp->next != NULL)
  8731.     hp->next->prev = hp;
  8732.   hp->type = type;
  8733.   hp->length = len;
  8734.   if (hp->type == T_CONST)
  8735.     hp->value.ival = ivalue;
  8736.   else
  8737.     hp->value.cpval = value;
  8738.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  8739.   p = hp->name;
  8740.   q = name;
  8741.   for (i = 0; i < len; i++)
  8742.     *p++ = *q++;
  8743.   hp->name[len] = 0;
  8744.   return hp;
  8745. }
  8746.  
  8747. /*
  8748.  * find the most recent hash node for name name (ending with first
  8749.  * non-identifier char) installed by install
  8750.  *
  8751.  * If LEN is >= 0, it is the length of the name.
  8752.  * Otherwise, compute the length by scanning the entire name.
  8753.  *
  8754.  * If HASH is >= 0, it is the precomputed hash code.
  8755.  * Otherwise, compute the hash code.
  8756.  */
  8757. HASHNODE *
  8758. lookup (name, len, hash)
  8759.      U_CHAR *name;
  8760.      int len;
  8761.      int hash;
  8762. {
  8763.   register U_CHAR *bp;
  8764.   register HASHNODE *bucket;
  8765.  
  8766.   if (len < 0) {
  8767.     for (bp = name; is_idchar[*bp]; bp++) ;
  8768.     len = bp - name;
  8769.   }
  8770.  
  8771.   if (hash < 0)
  8772.     hash = hashf (name, len, HASHSIZE);
  8773.  
  8774.   bucket = hashtab[hash];
  8775.   while (bucket) {
  8776.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  8777.       return bucket;
  8778.     bucket = bucket->next;
  8779.   }
  8780.   return NULL;
  8781. }
  8782.  
  8783. /*
  8784.  * Delete a hash node.  Some weirdness to free junk from macros.
  8785.  * More such weirdness will have to be added if you define more hash
  8786.  * types that need it.
  8787.  */
  8788.  
  8789. /* Note that the DEFINITION of a macro is removed from the hash table
  8790.    but its storage is not freed.  This would be a storage leak
  8791.    except that it is not reasonable to keep undefining and redefining
  8792.    large numbers of macros many times.
  8793.    In any case, this is necessary, because a macro can be #undef'd
  8794.    in the middle of reading the arguments to a call to it.
  8795.    If #undef freed the DEFINITION, that would crash.  */
  8796.  
  8797. static void
  8798. delete_macro (hp)
  8799.      HASHNODE *hp;
  8800. {
  8801.  
  8802.   if (hp->prev != NULL)
  8803.     hp->prev->next = hp->next;
  8804.   if (hp->next != NULL)
  8805.     hp->next->prev = hp->prev;
  8806.  
  8807.   /* make sure that the bucket chain header that
  8808.      the deleted guy was on points to the right thing afterwards. */
  8809.   if (hp == *hp->bucket_hdr)
  8810.     *hp->bucket_hdr = hp->next;
  8811.  
  8812. #if 0
  8813.   if (hp->type == T_MACRO) {
  8814.     DEFINITION *d = hp->value.defn;
  8815.     struct reflist *ap, *nextap;
  8816.  
  8817.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  8818.       nextap = ap->next;
  8819.       free (ap);
  8820.     }
  8821.     free (d);
  8822.   }
  8823. #endif
  8824.   free (hp);
  8825. }
  8826.  
  8827. /*
  8828.  * return hash function on name.  must be compatible with the one
  8829.  * computed a step at a time, elsewhere
  8830.  */
  8831. static int
  8832. hashf (name, len, hashsize)
  8833.      register U_CHAR *name;
  8834.      register int len;
  8835.      int hashsize;
  8836. {
  8837.   register int r = 0;
  8838.  
  8839.   while (len--)
  8840.     r = HASHSTEP (r, *name++);
  8841.  
  8842.   return MAKE_POS (r) % hashsize;
  8843. }
  8844.  
  8845.  
  8846. /* Dump the definition of a single macro HP to OF.  */
  8847. static void
  8848. dump_single_macro (hp, of)
  8849.      register HASHNODE *hp;
  8850.      FILE *of;
  8851. {
  8852.   register DEFINITION *defn = hp->value.defn;
  8853.   struct reflist *ap;
  8854.   int offset;
  8855.   int concat;
  8856.  
  8857.  
  8858.   /* Print the definition of the macro HP.  */
  8859.  
  8860.   fprintf (of, "#define %s", hp->name);
  8861.  
  8862.   if (defn->nargs >= 0) {
  8863.     int i;
  8864.  
  8865.     fprintf (of, "(");
  8866.     for (i = 0; i < defn->nargs; i++) {
  8867.       dump_arg_n (defn, i, of);
  8868.       if (i + 1 < defn->nargs)
  8869.     fprintf (of, ", ");
  8870.     }
  8871.     fprintf (of, ")");
  8872.   }
  8873.  
  8874.   fprintf (of, " ");
  8875.  
  8876.   offset = 0;
  8877.   concat = 0;
  8878.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  8879.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  8880.     if (ap->nchars != 0)
  8881.       concat = 0;
  8882.     offset += ap->nchars;
  8883.     if (ap->stringify)
  8884.       fprintf (of, " #");
  8885.     if (ap->raw_before && !concat)
  8886.       fprintf (of, " ## ");
  8887.     concat = 0;
  8888.     dump_arg_n (defn, ap->argno, of);
  8889.     if (ap->raw_after) {
  8890.       fprintf (of, " ## ");
  8891.       concat = 1;
  8892.     }
  8893.   }
  8894.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  8895.   fprintf (of, "\n");
  8896. }
  8897.  
  8898. /* Dump all macro definitions as #defines to stdout.  */
  8899.  
  8900. static void
  8901. dump_all_macros ()
  8902. {
  8903.   int bucket;
  8904.  
  8905.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  8906.     register HASHNODE *hp;
  8907.  
  8908.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  8909.       if (hp->type == T_MACRO)
  8910.     dump_single_macro (hp, stdout);
  8911.     }
  8912.   }
  8913. }
  8914.  
  8915. /* Output to OF a substring of a macro definition.
  8916.    BASE is the beginning of the definition.
  8917.    Output characters START thru LENGTH.
  8918.    Discard newlines outside of strings, thus
  8919.    converting funny-space markers to ordinary spaces.  */
  8920.  
  8921. static void
  8922. dump_defn_1 (base, start, length, of)
  8923.      U_CHAR *base;
  8924.      int start;
  8925.      int length;
  8926.      FILE *of;
  8927. {
  8928.   U_CHAR *p = base + start;
  8929.   U_CHAR *limit = base + start + length;
  8930.  
  8931.   while (p < limit) {
  8932.     if (*p != '\n')
  8933.       putc (*p, of);
  8934.     else if (*p == '\"' || *p =='\'') {
  8935.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  8936.                        NULL_PTR, NULL_PTR);
  8937.       fwrite (p, p1 - p, 1, of);
  8938.       p = p1 - 1;
  8939.     }
  8940.     p++;
  8941.   }
  8942. }
  8943.  
  8944. /* Print the name of argument number ARGNUM of macro definition DEFN
  8945.    to OF.
  8946.    Recall that DEFN->args.argnames contains all the arg names
  8947.    concatenated in reverse order with comma-space in between.  */
  8948.  
  8949. static void
  8950. dump_arg_n (defn, argnum, of)
  8951.      DEFINITION *defn;
  8952.      int argnum;
  8953.      FILE *of;
  8954. {
  8955.   register U_CHAR *p = defn->args.argnames;
  8956.   while (argnum + 1 < defn->nargs) {
  8957.     p = (U_CHAR *) index (p, ' ') + 1;
  8958.     argnum++;
  8959.   }
  8960.  
  8961.   while (*p && *p != ',') {
  8962.     putc (*p, of);
  8963.     p++;
  8964.   }
  8965. }
  8966.  
  8967. /* Initialize syntactic classifications of characters.  */
  8968.  
  8969. static void
  8970. initialize_char_syntax ()
  8971. {
  8972.   register int i;
  8973.  
  8974.   /*
  8975.    * Set up is_idchar and is_idstart tables.  These should be
  8976.    * faster than saying (is_alpha (c) || c == '_'), etc.
  8977.    * Set up these things before calling any routines tthat
  8978.    * refer to them.
  8979.    */
  8980.   for (i = 'a'; i <= 'z'; i++) {
  8981.     is_idchar[i - 'a' + 'A'] = 1;
  8982.     is_idchar[i] = 1;
  8983.     is_idstart[i - 'a' + 'A'] = 1;
  8984.     is_idstart[i] = 1;
  8985.   }
  8986.   for (i = '0'; i <= '9'; i++)
  8987.     is_idchar[i] = 1;
  8988.   is_idchar['_'] = 1;
  8989.   is_idstart['_'] = 1;
  8990.   is_idchar['$'] = dollars_in_ident;
  8991.   is_idstart['$'] = dollars_in_ident;
  8992.  
  8993.   /* horizontal space table */
  8994.   is_hor_space[' '] = 1;
  8995.   is_hor_space['\t'] = 1;
  8996.   is_hor_space['\v'] = 1;
  8997.   is_hor_space['\f'] = 1;
  8998.   is_hor_space['\r'] = 1;
  8999.  
  9000.   is_space[' '] = 1;
  9001.   is_space['\t'] = 1;
  9002.   is_space['\v'] = 1;
  9003.   is_space['\f'] = 1;
  9004.   is_space['\n'] = 1;
  9005.   is_space['\r'] = 1;
  9006. }
  9007.  
  9008. /* Initialize the built-in macros.  */
  9009.  
  9010. static void
  9011. initialize_builtins (inp, outp)
  9012.      FILE_BUF *inp;
  9013.      FILE_BUF *outp;
  9014. {
  9015.   install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
  9016.   install ("__DATE__", -1, T_DATE, 0, 0, -1);
  9017.   install ("__FILE__", -1, T_FILE, 0, 0, -1);
  9018.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
  9019.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
  9020.   install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
  9021. #ifndef NO_BUILTIN_SIZE_TYPE
  9022.   install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
  9023. #endif
  9024. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9025.   install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
  9026. #endif
  9027.   install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
  9028.   install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
  9029.   install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
  9030.   install ("__TIME__", -1, T_TIME, 0, 0, -1);
  9031.   if (!traditional)
  9032.     install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
  9033.   if (objc)
  9034.     install ("__OBJC__", -1, T_CONST, 1, 0, -1);
  9035. /*  This is supplied using a -D by the compiler driver
  9036.     so that it is present only when truly compiling with GNU C.  */
  9037. /*  install ("__GNUC__", -1, T_CONST, 2, 0, -1);  */
  9038.  
  9039.   if (debug_output)
  9040.     {
  9041.       char directive[2048];
  9042.       register struct directive *dp = &directive_table[0];
  9043.       struct tm *timebuf = timestamp ();
  9044.  
  9045.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  9046.            instack[0].nominal_fname);
  9047.       output_line_command (inp, outp, 0, same_file);
  9048.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9049.  
  9050.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  9051.       output_line_command (inp, outp, 0, same_file);
  9052.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9053.  
  9054. #ifndef NO_BUILTIN_SIZE_TYPE
  9055.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  9056.       output_line_command (inp, outp, 0, same_file);
  9057.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9058. #endif
  9059.  
  9060. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9061.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  9062.       output_line_command (inp, outp, 0, same_file);
  9063.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9064. #endif
  9065.  
  9066.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  9067.       output_line_command (inp, outp, 0, same_file);
  9068.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9069.  
  9070.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  9071.            monthnames[timebuf->tm_mon],
  9072.            timebuf->tm_mday, timebuf->tm_year + 1900);
  9073.       output_line_command (inp, outp, 0, same_file);
  9074.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9075.  
  9076.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  9077.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  9078.       output_line_command (inp, outp, 0, same_file);
  9079.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  9080.  
  9081.       if (!traditional)
  9082.     {
  9083.           sprintf (directive, " __STDC__ 1");
  9084.           output_line_command (inp, outp, 0, same_file);
  9085.           pass_thru_directive (directive, &directive[strlen (directive)],
  9086.                    outp, dp);
  9087.     }
  9088.       if (objc)
  9089.     {
  9090.           sprintf (directive, " __OBJC__ 1");
  9091.           output_line_command (inp, outp, 0, same_file);
  9092.           pass_thru_directive (directive, &directive[strlen (directive)],
  9093.                    outp, dp);
  9094.     }
  9095.     }
  9096. }
  9097.  
  9098. /*
  9099.  * process a given definition string, for initialization
  9100.  * If STR is just an identifier, define it with value 1.
  9101.  * If STR has anything after the identifier, then it should
  9102.  * be identifier=definition.
  9103.  */
  9104.  
  9105. static void
  9106. make_definition (str, op)
  9107.      U_CHAR *str;
  9108.      FILE_BUF *op;
  9109. {
  9110.   FILE_BUF *ip;
  9111.   struct directive *kt;
  9112.   U_CHAR *buf, *p;
  9113.  
  9114.   buf = str;
  9115.   p = str;
  9116.   if (!is_idstart[*p]) {
  9117.     error ("malformed option `-D %s'", str);
  9118.     return;
  9119.   }
  9120.   while (is_idchar[*++p])
  9121.     ;
  9122.   if (*p == 0) {
  9123.     buf = (U_CHAR *) alloca (p - buf + 4);
  9124.     strcpy ((char *)buf, str);
  9125.     strcat ((char *)buf, " 1");
  9126.   } else if (*p != '=') {
  9127.     error ("malformed option `-D %s'", str);
  9128.     return;
  9129.   } else {
  9130.     U_CHAR *q;
  9131.     /* Copy the entire option so we can modify it.  */
  9132.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  9133.     strncpy (buf, str, p - str);
  9134.     /* Change the = to a space.  */
  9135.     buf[p - str] = ' ';
  9136.     /* Scan for any backslash-newline and remove it.  */
  9137.     p++;
  9138.     q = &buf[p - str];
  9139.     while (*p) {
  9140.       if (*p == '\\' && p[1] == '\n')
  9141.     p += 2;
  9142.       /* Change newline chars into newline-markers.  */
  9143.       else if (*p == '\n')
  9144.     {
  9145.       *q++ = '\n';
  9146.       *q++ = '\n';
  9147.       p++;
  9148.     }
  9149.       else
  9150.     *q++ = *p++;
  9151.     }
  9152.     *q = 0;
  9153.   }
  9154.   
  9155.   ip = &instack[++indepth];
  9156.   ip->nominal_fname = ip->fname = "*Initialization*";
  9157.  
  9158.   ip->buf = ip->bufp = buf;
  9159.   ip->length = strlen (buf);
  9160.   ip->lineno = 1;
  9161.   ip->macro = 0;
  9162.   ip->free_ptr = 0;
  9163.   ip->if_stack = if_stack;
  9164.   ip->system_header_p = 0;
  9165.  
  9166.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  9167.     ;
  9168.  
  9169.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  9170.   do_define (buf, buf + strlen (buf), NULL, kt);
  9171.   --indepth;
  9172. }
  9173.  
  9174. /* JF, this does the work for the -U option */
  9175.  
  9176. static void
  9177. make_undef (str, op)
  9178.      U_CHAR *str;
  9179.      FILE_BUF *op;
  9180. {
  9181.   FILE_BUF *ip;
  9182.   struct directive *kt;
  9183.  
  9184.   ip = &instack[++indepth];
  9185.   ip->nominal_fname = ip->fname = "*undef*";
  9186.  
  9187.   ip->buf = ip->bufp = str;
  9188.   ip->length = strlen (str);
  9189.   ip->lineno = 1;
  9190.   ip->macro = 0;
  9191.   ip->free_ptr = 0;
  9192.   ip->if_stack = if_stack;
  9193.   ip->system_header_p = 0;
  9194.  
  9195.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  9196.     ;
  9197.  
  9198.   do_undef (str, str + strlen (str), op, kt);
  9199.   --indepth;
  9200. }
  9201.  
  9202. /* Process the string STR as if it appeared as the body of a #assert.
  9203.    OPTION is the option name for which STR was the argument.  */
  9204.  
  9205. static void
  9206. make_assertion (option, str)
  9207.      char *option;
  9208.      U_CHAR *str;
  9209. {
  9210.   FILE_BUF *ip;
  9211.   struct directive *kt;
  9212.   U_CHAR *buf, *p, *q;
  9213.  
  9214.   /* Copy the entire option so we can modify it.  */
  9215.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  9216.   strcpy ((char *) buf, str);
  9217.   /* Scan for any backslash-newline and remove it.  */
  9218.   p = q = buf;
  9219.   while (*p) {
  9220.     if (*p == '\\' && p[1] == '\n')
  9221.       p += 2;
  9222.     else
  9223.       *q++ = *p++;
  9224.   }
  9225.   *q = 0;
  9226.  
  9227.   p = buf;
  9228.   if (!is_idstart[*p]) {
  9229.     error ("malformed option `%s %s'", option, str);
  9230.     return;
  9231.   }
  9232.   while (is_idchar[*++p])
  9233.     ;
  9234.   while (*p == ' ' || *p == '\t') p++;
  9235.   if (! (*p == 0 || *p == '(')) {
  9236.     error ("malformed option `%s %s'", option, str);
  9237.     return;
  9238.   }
  9239.   
  9240.   ip = &instack[++indepth];
  9241.   ip->nominal_fname = ip->fname = "*Initialization*";
  9242.  
  9243.   ip->buf = ip->bufp = buf;
  9244.   ip->length = strlen (buf);
  9245.   ip->lineno = 1;
  9246.   ip->macro = 0;
  9247.   ip->free_ptr = 0;
  9248.   ip->if_stack = if_stack;
  9249.   ip->system_header_p = 0;
  9250.  
  9251.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  9252.     ;
  9253.  
  9254.   /* pass NULL as output ptr to do_define since we KNOW it never
  9255.      does any output.... */
  9256.   do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
  9257.   --indepth;
  9258. }
  9259.  
  9260. /* Append a chain of `struct file_name_list's
  9261.    to the end of the main include chain.
  9262.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  9263.  
  9264. static void
  9265. append_include_chain (first, last)
  9266.      struct file_name_list *first, *last;
  9267. {
  9268.   struct file_name_list *dir;
  9269.  
  9270.   if (!first || !last)
  9271.     return;
  9272.  
  9273.   if (include == 0)
  9274.     include = first;
  9275.   else
  9276.     last_include->next = first;
  9277.  
  9278.   if (first_bracket_include == 0)
  9279.     first_bracket_include = first;
  9280.  
  9281.   for (dir = first; ; dir = dir->next) {
  9282.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  9283.     if (len > max_include_len)
  9284.       max_include_len = len;
  9285.     if (dir == last)
  9286.       break;
  9287.   }
  9288.  
  9289.   last->next = NULL;
  9290.   last_include = last;
  9291. }
  9292.  
  9293. /* Add output to `deps_buffer' for the -M switch.
  9294.    STRING points to the text to be output.
  9295.    SPACER is ':' for targets, ' ' for dependencies, zero for text
  9296.    to be inserted literally.  */
  9297.  
  9298. static void
  9299. deps_output (string, spacer)
  9300.      char *string;
  9301.      int spacer;
  9302. {
  9303.   int size = strlen (string);
  9304.  
  9305.   if (size == 0)
  9306.     return;
  9307.  
  9308. #ifndef MAX_OUTPUT_COLUMNS
  9309. #define MAX_OUTPUT_COLUMNS 72
  9310. #endif
  9311.   if (spacer
  9312.       && deps_column > 0
  9313.       && (deps_column + size) > MAX_OUTPUT_COLUMNS)
  9314.   {
  9315.     deps_output (" \\\n  ", 0);
  9316.     deps_column = 0;
  9317.   }
  9318.  
  9319.   if (deps_size + size + 8 > deps_allocated_size) {
  9320.     deps_allocated_size = (deps_size + size + 50) * 2;
  9321.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  9322.   }
  9323.   if (spacer == ' ' && deps_column > 0)
  9324.     deps_buffer[deps_size++] = ' ';
  9325.   bcopy (string, &deps_buffer[deps_size], size);
  9326.   deps_size += size;
  9327.   deps_column += size;
  9328.   if (spacer == ':')
  9329.     deps_buffer[deps_size++] = ':';
  9330.   deps_buffer[deps_size] = 0;
  9331. }
  9332.  
  9333. #if defined(USG) || defined(VMS)
  9334. #ifndef BSTRING
  9335.  
  9336. void
  9337. bzero (b, length)
  9338.      register char *b;
  9339.      register unsigned length;
  9340. {
  9341.   while (length-- > 0)
  9342.     *b++ = 0;
  9343. }
  9344.  
  9345. void
  9346. bcopy (b1, b2, length)
  9347.      register char *b1;
  9348.      register char *b2;
  9349.      register unsigned length;
  9350. {
  9351.   while (length-- > 0)
  9352.     *b2++ = *b1++;
  9353. }
  9354.  
  9355. int
  9356. bcmp (b1, b2, length)    /* This could be a macro! */
  9357.      register char *b1;
  9358.      register char *b2;
  9359.      register unsigned length;
  9360. {
  9361.    while (length-- > 0)
  9362.      if (*b1++ != *b2++)
  9363.        return 1;
  9364.  
  9365.    return 0;
  9366. }
  9367. #endif /* not BSTRING */
  9368. #endif /* USG or VMS */
  9369.  
  9370.  
  9371. static void
  9372. fatal (str, arg)
  9373.      char *str, *arg;
  9374. {
  9375.   fprintf (stderr, "%s: ", progname);
  9376.   fprintf (stderr, str, arg);
  9377.   fprintf (stderr, "\n");
  9378.   exit (FAILURE_EXIT_CODE);
  9379. }
  9380.  
  9381. /* More 'friendly' abort that prints the line and file.
  9382.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  9383.  
  9384. void
  9385. fancy_abort ()
  9386. {
  9387.   fatal ("Internal gcc abort.");
  9388. }
  9389.  
  9390. static void
  9391. perror_with_name (name)
  9392.      char *name;
  9393. {
  9394.   fprintf (stderr, "%s: ", progname);
  9395.   if (errno < sys_nerr)
  9396.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  9397.   else
  9398.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  9399.   errors++;
  9400. }
  9401.  
  9402. static void
  9403. pfatal_with_name (name)
  9404.      char *name;
  9405. {
  9406.   perror_with_name (name);
  9407. #ifdef VMS
  9408.   exit (vaxc$errno);
  9409. #else
  9410.   exit (FAILURE_EXIT_CODE);
  9411. #endif
  9412. }
  9413.  
  9414. /* Handler for SIGPIPE.  */
  9415.  
  9416. static void
  9417. pipe_closed (signo)
  9418.      /* If this is missing, some compilers complain.  */
  9419.      int signo;
  9420. {
  9421.   fatal ("output pipe has been closed");
  9422. }
  9423.  
  9424. static void
  9425. memory_full ()
  9426. {
  9427.   fatal ("Memory exhausted.");
  9428. }
  9429.  
  9430.  
  9431. char *
  9432. xmalloc (size)
  9433.      unsigned size;
  9434. {
  9435.   register char *ptr = (char *) malloc (size);
  9436.   if (ptr != 0) return (ptr);
  9437.   memory_full ();
  9438.   /*NOTREACHED*/
  9439.   return 0;
  9440. }
  9441.  
  9442. static char *
  9443. xrealloc (old, size)
  9444.      char *old;
  9445.      unsigned size;
  9446. {
  9447.   register char *ptr = (char *) realloc (old, size);
  9448.   if (ptr != 0) return (ptr);
  9449.   memory_full ();
  9450.   /*NOTREACHED*/
  9451.   return 0;
  9452. }
  9453.  
  9454. static char *
  9455. xcalloc (number, size)
  9456.      unsigned number, size;
  9457. {
  9458.   register unsigned total = number * size;
  9459.   register char *ptr = (char *) malloc (total);
  9460.   if (ptr != 0) {
  9461.     if (total > 100)
  9462.       bzero (ptr, total);
  9463.     else {
  9464.       /* It's not too long, so loop, zeroing by longs.
  9465.      It must be safe because malloc values are always well aligned.  */
  9466.       register long *zp = (long *) ptr;
  9467.       register long *zl = (long *) (ptr + total - 4);
  9468.       register int i = total - 4;
  9469.       while (zp < zl)
  9470.     *zp++ = 0;
  9471.       if (i < 0)
  9472.     i = 0;
  9473.       while (i < total)
  9474.     ptr[i++] = 0;
  9475.     }
  9476.     return ptr;
  9477.   }
  9478.   memory_full ();
  9479.   /*NOTREACHED*/
  9480.   return 0;
  9481. }
  9482.  
  9483. static char *
  9484. savestring (input)
  9485.      char *input;
  9486. {
  9487.   unsigned size = strlen (input);
  9488.   char *output = xmalloc (size + 1);
  9489.   strcpy (output, input);
  9490.   return output;
  9491. }
  9492.  
  9493. /* Get the file-mode and data size of the file open on FD
  9494.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  9495.  
  9496. static int
  9497. file_size_and_mode (fd, mode_pointer, size_pointer)
  9498.      int fd;
  9499.      int *mode_pointer;
  9500.      long int *size_pointer;
  9501. {
  9502.   struct stat sbuf;
  9503.  
  9504.   if (fstat (fd, &sbuf) < 0) return (-1);
  9505.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  9506.   if (size_pointer) *size_pointer = sbuf.st_size;
  9507.   return 0;
  9508. }
  9509.  
  9510. static void
  9511. output_dots (fd, depth)
  9512.      FILE* fd;
  9513.      int depth;
  9514. {
  9515.   while (depth > 0) {
  9516.     putc ('.', fd);
  9517.     depth--;
  9518.   }
  9519. }
  9520.   
  9521.  
  9522. #ifdef VMS
  9523.  
  9524. /* Under VMS we need to fix up the "include" specification
  9525.    filename so that everything following the 1st slash is
  9526.    changed into its correct VMS file specification. */
  9527.  
  9528. static void
  9529. hack_vms_include_specification (fname)
  9530.      char *fname;
  9531. {
  9532.   register char *cp, *cp1, *cp2;
  9533.   int f, check_filename_before_returning, no_prefix_seen;
  9534.   char Local[512];
  9535.  
  9536.   check_filename_before_returning = 0;
  9537.   no_prefix_seen = 0;
  9538.  
  9539.   /* Ignore leading "./"s */
  9540.   while (fname[0] == '.' && fname[1] == '/') {
  9541.     strcpy (fname, fname+2);
  9542.     no_prefix_seen = 1;        /* mark this for later */
  9543.   }
  9544.   /* Look for the boundary between the VMS and UNIX filespecs */
  9545.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  9546.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  9547.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  9548.   if (cp) {
  9549.     cp++;
  9550.   } else {
  9551.     cp = index (fname, '/');    /* Look for the "/" */
  9552.   }
  9553.  
  9554.   /*
  9555.    * Check if we have a vax-c style '#include filename'
  9556.    * and add the missing .h
  9557.    */
  9558.   if (cp == 0) {
  9559.     if (index(fname,'.') == 0)
  9560.       strcat(fname, ".h");
  9561.   } else {
  9562.     if (index(cp,'.') == 0)
  9563.       strcat(cp, ".h");
  9564.   }
  9565.  
  9566.   cp2 = Local;            /* initialize */
  9567.  
  9568.   /* We are trying to do a number of things here.  First of all, we are
  9569.      trying to hammer the filenames into a standard format, such that later
  9570.      processing can handle them.
  9571.      
  9572.      If the file name contains something like [dir.], then it recognizes this
  9573.      as a root, and strips the ".]".  Later processing will add whatever is
  9574.      needed to get things working properly.
  9575.      
  9576.      If no device is specified, then the first directory name is taken to be
  9577.      a device name (or a rooted logical). */
  9578.  
  9579.   /* See if we found that 1st slash */
  9580.   if (cp == 0) return;        /* Nothing to do!!! */
  9581.   if (*cp != '/') return;    /* Nothing to do!!! */
  9582.   /* Point to the UNIX filename part (which needs to be fixed!) */
  9583.   cp1 = cp+1;
  9584.   /* If the directory spec is not rooted, we can just copy
  9585.      the UNIX filename part and we are done */
  9586.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  9587.     if (cp[-2] != '.') {
  9588.       /*
  9589.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  9590.        * We strip the `]', and then splice the two parts of the name in the
  9591.        * usual way.  Given the default locations for include files in cccp.c,
  9592.        * we will only use this code if the user specifies alternate locations
  9593.        * with the /include (-I) switch on the command line.  */
  9594.       cp -= 1;            /* Strip "]" */
  9595.       cp1--;            /* backspace */
  9596.     } else {
  9597.       /*
  9598.        * The VMS part has a ".]" at the end, and this will not do.  Later
  9599.        * processing will add a second directory spec, and this would be a syntax
  9600.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  9601.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  9602.        * generation of the 000000 root directory spec (which does not belong here
  9603.        * in this case).
  9604.        */
  9605.       cp -= 2;            /* Strip ".]" */
  9606.       cp1--; };            /* backspace */
  9607.   } else {
  9608.  
  9609.     /* We drop in here if there is no VMS style directory specification yet.
  9610.      * If there is no device specification either, we make the first dir a
  9611.      * device and try that.  If we do not do this, then we will be essentially
  9612.      * searching the users default directory (as if they did a #include "asdf.h").
  9613.      *
  9614.      * Then all we need to do is to push a '[' into the output string. Later
  9615.      * processing will fill this in, and close the bracket.
  9616.      */
  9617.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  9618.     *cp2++ = '[';        /* Open the directory specification */
  9619.   }
  9620.  
  9621.   /* at this point we assume that we have the device spec, and (at least
  9622.      the opening "[" for a directory specification.  We may have directories
  9623.      specified already */
  9624.  
  9625.   /* If there are no other slashes then the filename will be
  9626.      in the "root" directory.  Otherwise, we need to add
  9627.      directory specifications. */
  9628.   if (index (cp1, '/') == 0) {
  9629.     /* Just add "000000]" as the directory string */
  9630.     strcpy (cp2, "000000]");
  9631.     cp2 += strlen (cp2);
  9632.     check_filename_before_returning = 1; /* we might need to fool with this later */
  9633.   } else {
  9634.     /* As long as there are still subdirectories to add, do them. */
  9635.     while (index (cp1, '/') != 0) {
  9636.       /* If this token is "." we can ignore it */
  9637.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  9638.     cp1 += 2;
  9639.     continue;
  9640.       }
  9641.       /* Add a subdirectory spec. Do not duplicate "." */
  9642.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  9643.     *cp2++ = '.';
  9644.       /* If this is ".." then the spec becomes "-" */
  9645.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  9646.     /* Add "-" and skip the ".." */
  9647.     *cp2++ = '-';
  9648.     cp1 += 3;
  9649.     continue;
  9650.       }
  9651.       /* Copy the subdirectory */
  9652.       while (*cp1 != '/') *cp2++= *cp1++;
  9653.       cp1++;            /* Skip the "/" */
  9654.     }
  9655.     /* Close the directory specification */
  9656.     if (cp2[-1] == '.')        /* no trailing periods */
  9657.       cp2--;
  9658.     *cp2++ = ']';
  9659.   }
  9660.   /* Now add the filename */
  9661.   while (*cp1) *cp2++ = *cp1++;
  9662.   *cp2 = 0;
  9663.   /* Now append it to the original VMS spec. */
  9664.   strcpy (cp, Local);
  9665.  
  9666.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  9667.      remove the [000000], and return that name.  This provides flexibility
  9668.      to the user in that they can use both rooted and non-rooted logical names
  9669.      to point to the location of the file.  */
  9670.  
  9671.   if (check_filename_before_returning && no_prefix_seen) {
  9672.     f = open (fname, O_RDONLY, 0666);
  9673.     if (f >= 0) {
  9674.       /* The file name is OK as it is, so return it as is.  */
  9675.       close (f);
  9676.       return;
  9677.     }
  9678.     /* The filename did not work.  Try to remove the [000000] from the name,
  9679.        and return it.  */
  9680.     cp = index (fname, '[');
  9681.     cp2 = index (fname, ']') + 1;
  9682.     strcpy (cp, cp2);        /* this gets rid of it */
  9683.   }
  9684.   return;
  9685. }
  9686. #endif    /* VMS */
  9687.  
  9688. #ifdef    VMS
  9689.  
  9690. /* These are the read/write replacement routines for
  9691.    VAX-11 "C".  They make read/write behave enough
  9692.    like their UNIX counterparts that CCCP will work */
  9693.  
  9694. static int
  9695. read (fd, buf, size)
  9696.      int fd;
  9697.      char *buf;
  9698.      int size;
  9699. {
  9700. #undef    read    /* Get back the REAL read routine */
  9701.   register int i;
  9702.   register int total = 0;
  9703.  
  9704.   /* Read until the buffer is exhausted */
  9705.   while (size > 0) {
  9706.     /* Limit each read to 32KB */
  9707.     i = (size > (32*1024)) ? (32*1024) : size;
  9708.     i = read (fd, buf, i);
  9709.     if (i <= 0) {
  9710.       if (i == 0) return (total);
  9711.       return (i);
  9712.     }
  9713.     /* Account for this read */
  9714.     total += i;
  9715.     buf += i;
  9716.     size -= i;
  9717.   }
  9718.   return (total);
  9719. }
  9720.  
  9721. static int
  9722. write (fd, buf, size)
  9723.      int fd;
  9724.      char *buf;
  9725.      int size;
  9726. {
  9727. #undef    write    /* Get back the REAL write routine */
  9728.   int i;
  9729.   int j;
  9730.  
  9731.   /* Limit individual writes to 32Kb */
  9732.   i = size;
  9733.   while (i > 0) {
  9734.     j = (i > (32*1024)) ? (32*1024) : i;
  9735.     if (write (fd, buf, j) < 0) return (-1);
  9736.     /* Account for the data written */
  9737.     buf += j;
  9738.     i -= j;
  9739.   }
  9740.   return (size);
  9741. }
  9742.  
  9743. /* The following wrapper functions supply additional arguments to the VMS
  9744.    I/O routines to optimize performance with file handling.  The arguments
  9745.    are:
  9746.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  9747.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  9748.      "fop=tef"- Truncate unused portions of file when closing file.
  9749.      "shr=nil"- Disallow file sharing while file is open.
  9750.  */
  9751.  
  9752. static FILE *
  9753. freopen (fname, type, oldfile)
  9754.      char *fname;
  9755.      char *type;
  9756.      FILE *oldfile;
  9757. {
  9758. #undef    freopen    /* Get back the REAL fopen routine */
  9759.   if (strcmp (type, "w") == 0)
  9760.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  9761.   return freopen (fname, type, oldfile, "mbc=16");
  9762. }
  9763.  
  9764. static FILE *
  9765. fopen (fname, type)
  9766.      char *fname;
  9767.      char *type;
  9768. {
  9769. #undef fopen    /* Get back the REAL fopen routine */
  9770.   if (strcmp (type, "w") == 0)
  9771.     return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  9772.   return fopen (fname, type, "mbc=16");
  9773. }
  9774.  
  9775. static int 
  9776. open (fname, flags, prot)
  9777.      char *fname;
  9778.      int flags;
  9779.      int prot;
  9780. {
  9781. #undef open    /* Get back the REAL open routine */
  9782.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  9783. }
  9784.  
  9785. /* Avoid run-time library bug, where copying M out of N+M characters with
  9786.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  9787.    gcc-cpp exercises this particular bug.  */
  9788.  
  9789. static char *
  9790. strncat (dst, src, cnt)
  9791.      char *dst;
  9792.      const char *src;
  9793.      unsigned cnt;
  9794. {
  9795.   register char *d = dst, *s = (char *) src;
  9796.   register int n = cnt;    /* convert to _signed_ type */
  9797.  
  9798.   while (*d) d++;    /* advance to end */
  9799.   while (--n >= 0)
  9800.     if (!(*d++ = *s++)) break;
  9801.   if (n < 0) *d = '\0';
  9802.   return dst;
  9803. }
  9804. #endif /* VMS */
  9805.